我有这个字段,
job_created = models.DateTimeField(auto_now_add=True)
现在我想添加这样的内容,如何基于post_created本身设置默认值。我希望第一个last_modified是工作创建时,我想要像这样的东西。
last_modified = models.DateTimeField(auto_now=True, default = post_created)
如果我在运行迁移时提供一个默认值,该怎么做?
我有这个字段,
job_created = models.DateTimeField(auto_now_add=True)
last_modified = models.DateTimeField(auto_now=True, default = post_created)
如果我在运行迁移时提供一个默认值,该怎么做?
def set_last_modified(apps, schema_editor):
MyModel = apps.get_model('myapp', 'MyModel')
for obj in MyModel.objects.all():
obj.last_modified = obj.post_created
obj.save()
class Migration(migrations.Migration):
dependencies = [
('myapp', 'previous_migration'),
]
operations = [
# Doesn't need to do anything in reverse, models have not been changed
migrations.RunPython(set_last_modified, migrations.RunPython.noop)
]
migrations --empty yourappname