Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

[1.7.x] Fixed #22351 -- Removed usage of lambdas in model field options.

Thanks claudep for review.

Backport of 5ebf03b from master
  • Loading branch information...
commit 9673013abe4cd64183bf55d89adfa04927e3b947 1 parent e548328
@timgraham timgraham authored
Showing with 13 additions and 3 deletions.
  1. +13 −3 docs/ref/models/fields.txt
16 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": ""})
+ def contact_default():
+ return {"email": ""}
+ 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.
@@ -1106,7 +1113,10 @@ define the details of how the relation works.
with the Python ``datetime`` module to limit selections by date range. For
- limit_choices_to = lambda: {'pub_date__lte':}
+ def limit_pub_date_choices():
+ return {'pub_date__lte':}
+ 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

0 comments on commit 9673013

Please sign in to comment.
Something went wrong with that request. Please try again.