[Django] #36573: "fields.E010" should not warn when field defaults are expressions

6 views
Skip to first unread message

Django

unread,
Aug 26, 2025, 10:18:30 AMAug 26
to django-...@googlegroups.com
#36573: "fields.E010" should not warn when field defaults are expressions
-------------------------------------+-------------------------------------
Reporter: Clifford | Owner: Clifford Gama
Gama |
Type: | Status: assigned
Cleanup/optimization |
Component: Core | Version: dev
(System checks) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The
[https://github.com/django/django/blob/3e7aedfb2eb37d0c83ce3ce936f1b89eb5451dfa/django/db/models/fields/mixins.py#L40
CheckFieldDefaultMixin._check_default()] system check (fields.E010)
currently warns whenever a field’s default is not None, and not callable.
This is meant to prevent accidental sharing of mutable Python objects such
as `[]` or `{}` across all model instances, however this also guards
against expressions.

For example, it warns against using `data =
models.JSONField(default=Value({"key": "value"}, JSONField()))` and has
been warning for `Value(None, JSONField())`. This seems unnecessary,
because (unless I’m missing something) expressions are not subject to the
mutability/sharing problem that the check is meant to prevent.
--
Ticket URL: <https://code.djangoproject.com/ticket/36573>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 26, 2025, 11:57:59 AMAug 26
to django-...@googlegroups.com
#36573: "fields.E010" should not warn when field defaults are expressions
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* has_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36573#comment:1>

Django

unread,
Aug 27, 2025, 5:50:35 AMAug 27
to django-...@googlegroups.com
#36573: "fields.E010" should not warn when field defaults are expressions
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tim Graham):

`Field.db_default` supports expressions, but I don't think `Field.default`
does. At least, it's not documented.
--
Ticket URL: <https://code.djangoproject.com/ticket/36573#comment:2>

Django

unread,
Aug 28, 2025, 10:26:55 AMAug 28
to django-...@googlegroups.com
#36573: "fields.E010" should not warn when field defaults are expressions
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution: wontfix
Keywords: expression, default | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* cc: Sage Abdullah (added)
* keywords: => expression, default
* resolution: => wontfix
* status: assigned => closed

Comment:

This check only runs on `JSONField` and the postgres fields `ArrayField`
and `HStoreField`. I think you're right that `Value` isn't really what
this check is intended to complain about.

To Tim's point, though, we probably want this doc'd and tested before
encouraging further use. To me that means a new features repo ticket. For
instance, once we doc & test support for expressions in `default`, we may
find we want a system check for all fields, not just JSONField and
friends, flagging inappropriate kinds of expressions. For instance, `F()`
expressions don't really work as a default:

{{{
ValueError: Failed to insert expression "Col(plugins, models.Plugin.name)"
on models.Plugin.slug. F() expressions can only be used to update, not to
insert.
}}}

wontfix'ing pending a new-features ticket to explore extent of support
(which would be a great idea!)
--
Ticket URL: <https://code.djangoproject.com/ticket/36573#comment:3>

Django

unread,
Sep 1, 2025, 5:41:54 AMSep 1
to django-...@googlegroups.com
#36573: "fields.E010" should not warn when field defaults are expressions
-------------------------------------+-------------------------------------
Reporter: Clifford Gama | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: closed
Component: Core (System | Version: dev
checks) |
Severity: Normal | Resolution: duplicate
Keywords: expression, default | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* resolution: wontfix => duplicate

Comment:

I suppose this makes this a duplicate of #30032.
--
Ticket URL: <https://code.djangoproject.com/ticket/36573#comment:4>
Reply all
Reply to author
Forward
0 new messages