Django - 是否有可能在创建模型时添加默认值?
Django - Is this possible that the add default values when model is creating?
我想在使用 makemigrations 命令创建相关模型时在我的数据库中添加一些默认值。
例如我有这个作为模型;
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Created Date')
modified_at = models.DateTimeField(auto_now=True, verbose_name='Update Date')
is_deleted = models.BooleanField(default=False, verbose_name='Deleted')
class Meta:
abstract = True
class ModelType(BaseModel):
description = models.CharField(verbose_name='Name', max_length=225 )
正如我之前所说,我想为我的模型类型 table 添加一些默认值("value1"、"value2"、"value3"、"value4") .这可能吗?
显然有办法。您可以使用固定装置用数据初始化模型。
参考这篇文档:https://docs.djangoproject.com/en/1.10/howto/initial-data/
如果您想在执行给定迁移时始终添加默认数据,最安全的方法是使用数据迁移(如@Kos 所建议)。
要创建数据迁移,请使用 ./manage.py makemigrations <app_label> --empty
并手动添加所需的代码来填充数据。
我通常使用在指定模型上执行 get_or_create 的自定义操作。将此代码添加到迁移文件本身或可以从中导入它的地方:
from django.db import migrations
def noop(apps, schema_editor):
pass
class EnsureInstanceCreated(migrations.RunPython):
def __init__(self, app_name, model_name, attrs, defaults=None):
super(EnsureInstanceCreated, self).__init__(self.add_instance, noop)
self.app_name = app_name
self.model_name = model_name
self.attrs = attrs
self.defaults = defaults
def add_instance(self, apps, schema_editor):
Model = apps.get_model(self.app_name, self.model_name)
Model.objects.get_or_create(
defaults=self.defaults,
**self.attrs
)
然后,在迁移本身中:
from django.db import migrations
from myproject.utils.migrations import EnsureInstanceCreated
class Migration(migrations.Migration):
dependencies = [
('myproject', '000x_auto_...'),
]
operations = [
EnsureInstanceCreated('myapp', 'ModelType', attrs={
'description': 'value1',
}, defaults={
# ...
}),
EnsureInstanceCreated('myapp', 'ModelType', attrs={'description': 'value2'}),
EnsureInstanceCreated('myapp', 'ModelType', {'description': 'value3'}),
]
我想在使用 makemigrations 命令创建相关模型时在我的数据库中添加一些默认值。
例如我有这个作为模型;
class BaseModel(models.Model):
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Created Date')
modified_at = models.DateTimeField(auto_now=True, verbose_name='Update Date')
is_deleted = models.BooleanField(default=False, verbose_name='Deleted')
class Meta:
abstract = True
class ModelType(BaseModel):
description = models.CharField(verbose_name='Name', max_length=225 )
正如我之前所说,我想为我的模型类型 table 添加一些默认值("value1"、"value2"、"value3"、"value4") .这可能吗?
显然有办法。您可以使用固定装置用数据初始化模型。
参考这篇文档:https://docs.djangoproject.com/en/1.10/howto/initial-data/
如果您想在执行给定迁移时始终添加默认数据,最安全的方法是使用数据迁移(如@Kos 所建议)。
要创建数据迁移,请使用 ./manage.py makemigrations <app_label> --empty
并手动添加所需的代码来填充数据。
我通常使用在指定模型上执行 get_or_create 的自定义操作。将此代码添加到迁移文件本身或可以从中导入它的地方:
from django.db import migrations
def noop(apps, schema_editor):
pass
class EnsureInstanceCreated(migrations.RunPython):
def __init__(self, app_name, model_name, attrs, defaults=None):
super(EnsureInstanceCreated, self).__init__(self.add_instance, noop)
self.app_name = app_name
self.model_name = model_name
self.attrs = attrs
self.defaults = defaults
def add_instance(self, apps, schema_editor):
Model = apps.get_model(self.app_name, self.model_name)
Model.objects.get_or_create(
defaults=self.defaults,
**self.attrs
)
然后,在迁移本身中:
from django.db import migrations
from myproject.utils.migrations import EnsureInstanceCreated
class Migration(migrations.Migration):
dependencies = [
('myproject', '000x_auto_...'),
]
operations = [
EnsureInstanceCreated('myapp', 'ModelType', attrs={
'description': 'value1',
}, defaults={
# ...
}),
EnsureInstanceCreated('myapp', 'ModelType', attrs={'description': 'value2'}),
EnsureInstanceCreated('myapp', 'ModelType', {'description': 'value3'}),
]