Ticket #22351: 22351.diff

File 22351.diff, 1.6 KB (added by Tim Graham, 4 years ago)
  • docs/ref/models/fields.txt

    diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
    index 21934cf..a99522c 100644
    a b The default cannot be a mutable object (model instance, list, set, etc.), as a 
    209209reference to the same instance of that object would be used as the default
    210210value in all new model instances. Instead, wrap the desired default in a
    211211callable.  For example, if you had a custom ``JSONField`` and wanted to specify
    212 a dictionary as the default, use a ``lambda`` as follows::
     212a dictionary as the default, use a function as follows::
    214     contact_info = JSONField("ContactInfo", default=lambda:{"email": "to1@example.com"})
     214    def contact_default():
     215        return {"email": "to1@example.com"}
     217    contact_info = JSONField("ContactInfo", default=contact_default)
     219Note that ``lambda``\s cannot be used for field options like ``default``
     220because they cannot be :ref:`serialized by migrations <migration-serializing>`.
     221See that documentation for other caveats.
    define the details of how the relation works. 
    11011108    with the Python ``datetime`` module to limit selections by date range. For
    11021109    example::
    1104         limit_choices_to = lambda: {'pub_date__lte': datetime.date.utcnow()}
     1111        def limit_pub_date_choices():
     1112            return {'pub_date__lte': datetime.date.utcnow()}
     1114        limit_choices_to = limit_pub_date_choices
    11061116    If ``limit_choices_to`` is or returns a :class:`Q object
    11071117    <django.db.models.Q>`, which is useful for :ref:`complex queries
Back to Top