diff options
| author | Tim Graham <timograham@gmail.com> | 2012-09-08 14:52:17 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2012-09-08 14:54:07 -0400 |
| commit | cd5181f84c7d4f2002efb777b6cf042771135f7a (patch) | |
| tree | e8b314adf6ea5bde09b1cf35f32550b4ecd57bec /docs/ref | |
| parent | b0e2cb8e470acc552b4ed3fde3d5b1322b426bf2 (diff) | |
[1.4.X] Fixed #18478 - Documented how to use a mutable default in a model field.
Backport of 6e2bb344e4 from master
Diffstat (limited to 'docs/ref')
| -rw-r--r-- | docs/ref/models/fields.txt | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index aadfe25ae0..a38fc96fb8 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -197,6 +197,14 @@ support tablespaces for indexes, this option is ignored. 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 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:: + + contact_info = JSONField("ContactInfo", default=lambda:{"email": "to1@example.com"}) + ``editable`` ------------ |
