The default value is used when new model instances are created and a value isn’t provided for the field. When the field is a primary key, the default is also used when the field is set to None.
因此,不能直接将现有实例的字段设置为其默认值。但你可能会发现一个窍门:
FOO_XYZ_DEFAULT = 122
class Foo(models.Model):
xyz = models.IntegerField(default=FOO_XYZ_DEFAULT)
# [...]
a = Foo.objects.get(...)
a.xyz = FOO_XYZ_DEFAULT
从Django docs:
因此,不能直接将现有实例的字段设置为其默认值。但你可能会发现一个窍门:
如果默认值是可调用的(lambda、function等),您还可以将该值重置为该可调用的结果
相关问题 更多 >
编程相关推荐