If I have the following model in django;
如果我在django中有以下模型;
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100,default=name)
How do I make the fullname field default to name? As it is right now, the fullname defaults to the string representation of the name CharField.
如何使fullname字段默认为name?就像现在一样,fullname默认为CharField名称的字符串表示形式。
Example:
new MyModel(name='joel')
would yield 'joel' as both name and fullname, whereas
会产生'joel'作为名字和全名,而
new MyModel(name='joel',fullname='joel karlsson')
would yield different name and fullname.
会产生不同的名称和全名。
2 个解决方案
#1
7
I wonder if you're better off doing this via a method on your model:
我想知道你是否最好通过模型上的方法做到这一点:
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100)
def display_name(self):
if self.fullname:
return self.fullname
return self.name
Perhaps, instead of display_name
this should be your __unicode__
method.
也许,而不是display_name,这应该是你的__unicode__方法。
If you really want to do what you've asked though, then you can't do this using the default
- use the clean
method on your form instead (or your model, if you're using new-fangled model validation (available since Django 1.2).
如果你真的想做你曾经问过的事情,那么你不能使用默认设置 - 在你的表格上使用clean方法(或者你的模型,如果你使用的是新模型验证) Django 1.2)。
Something like this (for model validation):
这样的东西(用于模型验证):
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100,default=name)
def clean(self):
self.fullname=name
Or like this (for form validation):
或者像这样(用于表单验证):
class MyModelForm(ModelForm):
class Meta:
model = MyModel
def clean(self):
cleaned_data = self.cleaned_data
cleaned_data['fullname'] = cleaned_data['name']
return cleaned_data
#2
1
How about making a migration with a default value, and then adding custom data migration to your migration file? Here's a complete migration file example:
如何使用默认值进行迁移,然后将自定义数据迁移添加到迁移文件中?这是一个完整的迁移文件示例:
from datetime import timedelta
from django.db import migrations, models
import django.utils.timezone
# noinspection PyUnusedLocal
def set_free_credits_added_on(apps, schema_editor):
# noinspection PyPep8Naming
UserProfile = apps.get_model('core', 'UserProfile')
for user_profile in UserProfile.objects.all():
user_profile.free_credits_added_on = user_profile.next_billing - timedelta(days=30)
user_profile.save()
# noinspection PyUnusedLocal
def do_nothing(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('core', '0078_auto_20171104_0659'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='free_credits_added_on',
# This default value is overridden in the following data migration code
field=models.DateTimeField(
auto_now_add=True,
default=django.utils.timezone.now,
verbose_name='Free Credits Added On'
),
preserve_default=False,
),
migrations.RunPython(code=set_free_credits_added_on, reverse_code=do_nothing),
]
Here the free_credits_added_on
field is set to 30 days before the existing next_billing
field.
这里free_credits_added_on字段设置为现有next_billing字段之前30天。
#1
7
I wonder if you're better off doing this via a method on your model:
我想知道你是否最好通过模型上的方法做到这一点:
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100)
def display_name(self):
if self.fullname:
return self.fullname
return self.name
Perhaps, instead of display_name
this should be your __unicode__
method.
也许,而不是display_name,这应该是你的__unicode__方法。
If you really want to do what you've asked though, then you can't do this using the default
- use the clean
method on your form instead (or your model, if you're using new-fangled model validation (available since Django 1.2).
如果你真的想做你曾经问过的事情,那么你不能使用默认设置 - 在你的表格上使用clean方法(或者你的模型,如果你使用的是新模型验证) Django 1.2)。
Something like this (for model validation):
这样的东西(用于模型验证):
class MyModel(models.Model):
name = models.CharField(max_length=50)
fullname = models.CharField(max_length=100,default=name)
def clean(self):
self.fullname=name
Or like this (for form validation):
或者像这样(用于表单验证):
class MyModelForm(ModelForm):
class Meta:
model = MyModel
def clean(self):
cleaned_data = self.cleaned_data
cleaned_data['fullname'] = cleaned_data['name']
return cleaned_data
#2
1
How about making a migration with a default value, and then adding custom data migration to your migration file? Here's a complete migration file example:
如何使用默认值进行迁移,然后将自定义数据迁移添加到迁移文件中?这是一个完整的迁移文件示例:
from datetime import timedelta
from django.db import migrations, models
import django.utils.timezone
# noinspection PyUnusedLocal
def set_free_credits_added_on(apps, schema_editor):
# noinspection PyPep8Naming
UserProfile = apps.get_model('core', 'UserProfile')
for user_profile in UserProfile.objects.all():
user_profile.free_credits_added_on = user_profile.next_billing - timedelta(days=30)
user_profile.save()
# noinspection PyUnusedLocal
def do_nothing(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('core', '0078_auto_20171104_0659'),
]
operations = [
migrations.AddField(
model_name='userprofile',
name='free_credits_added_on',
# This default value is overridden in the following data migration code
field=models.DateTimeField(
auto_now_add=True,
default=django.utils.timezone.now,
verbose_name='Free Credits Added On'
),
preserve_default=False,
),
migrations.RunPython(code=set_free_credits_added_on, reverse_code=do_nothing),
]
Here the free_credits_added_on
field is set to 30 days before the existing next_billing
field.
这里free_credits_added_on字段设置为现有next_billing字段之前30天。