Made cosmetic edits to Field.default docs.

This commit is contained in:
Victor Oliveira da Silva 2016-08-13 22:56:21 -03:00 committed by Tim Graham
parent d7e0cf04b7
commit d7804662f8
1 changed files with 7 additions and 7 deletions

View File

@ -209,20 +209,20 @@ support tablespaces for indexes, this option is ignored.
The default value for the field. This can be a value or a callable object. If 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. callable it will be called every time a new object is created.
The default cannot be a mutable object (model instance, list, set, etc.), as a The default can't be a mutable object (model instance, ``list``, ``set``, etc.),
reference to the same instance of that object would be used as the default 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 value in all new model instances. Instead, wrap the desired default in a
callable. For example, if you had a custom ``JSONField`` and wanted to specify callable. For example, if you want to specify a default ``dict`` for
a dictionary as the default, use a function as follows:: :class:`~django.contrib.postgres.fields.JSONField`, use a function::
def contact_default(): def contact_default():
return {"email": "to1@example.com"} return {"email": "to1@example.com"}
contact_info = JSONField("ContactInfo", default=contact_default) contact_info = JSONField("ContactInfo", default=contact_default)
Note that ``lambda``\s cannot be used for field options like ``default`` ``lambda``\s can't be used for field options like ``default`` because they
because they cannot be :ref:`serialized by migrations <migration-serializing>`. can't be :ref:`serialized by migrations <migration-serializing>`. See that
See that documentation for other caveats. documentation for other caveats.
For fields like :class:`ForeignKey` that map to model instances, defaults For fields like :class:`ForeignKey` that map to model instances, defaults
should be the value of the field they reference (``pk`` unless should be the value of the field they reference (``pk`` unless