diff options
| author | Tim Graham <timograham@gmail.com> | 2014-07-09 07:31:50 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-07-09 07:31:50 -0400 |
| commit | 5ebf03b7dd2d1c215f5c0b725083d36379a7ac5b (patch) | |
| tree | 8b0d44a40d4a263ad456771d3c9b8911d488d322 /docs/ref | |
| parent | e167e96cfea670422ca75d0b35fe7c4195f25b63 (diff) | |
Fixed #22351 -- Removed usage of lambdas in model field options.
Thanks claudep for review.
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/fields.txt | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 21934cf13d..a99522c77c 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -209,9 +209,16 @@ The default cannot 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 had a custom ``JSONField`` and wanted to specify -a dictionary as the default, use a ``lambda`` as follows:: +a dictionary as the default, use a function as follows:: - contact_info = JSONField("ContactInfo", default=lambda:{"email": "to1@example.com"}) + def contact_default(): + return {"email": "to1@example.com"} + + contact_info = JSONField("ContactInfo", default=contact_default) + +Note that ``lambda``\s cannot be used for field options like ``default`` +because they cannot be :ref:`serialized by migrations <migration-serializing>`. +See that documentation for other caveats. ``editable`` ------------ @@ -1101,7 +1108,10 @@ define the details of how the relation works. with the Python ``datetime`` module to limit selections by date range. For example:: - limit_choices_to = lambda: {'pub_date__lte': datetime.date.utcnow()} + def limit_pub_date_choices(): + return {'pub_date__lte': datetime.date.utcnow()} + + limit_choices_to = limit_pub_date_choices If ``limit_choices_to`` is or returns a :class:`Q object <django.db.models.Q>`, which is useful for :ref:`complex queries |
