Field.default
The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.
The default can’t be a mutable object (model instance, list
, set
, etc.), as a reference to the same instance of that object would be used as the default value in all new model instances. Instead, wrap the desired default in a callable. For example, if you want to specify a default dict
for JSONField
, use a function:
def contact_default(): return {"email": "to1@example.com"} contact_info = JSONField("ContactInfo", default=contact_default)
lambda
s can’t be used for field options like default
because they can’t be serialized by migrations. See that documentation for other caveats.
For fields like ForeignKey
that map to model instances, defaults should be the value of the field they reference (pk
unless to_field
is set) instead of model instances.
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
.
Please login to continue.