#36573 closed Cleanup/optimization (duplicate)
"fields.E010" should not warn when field defaults are expressions
Reported by: | Clifford Gama | Owned by: | Clifford Gama |
---|---|---|---|
Component: | Core (System checks) | Version: | dev |
Severity: | Normal | Keywords: | expression, default |
Cc: | Sage Abdullah | Triage Stage: | Unreviewed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The 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.
Change History (4)
comment:1 by , 6 days ago
Has patch: | set |
---|
comment:2 by , 6 days ago
comment:3 by , 5 days ago
Cc: | added |
---|---|
Keywords: | expression default added |
Resolution: | → wontfix |
Status: | assigned → closed |
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!)
comment:4 by , 17 hours ago
Resolution: | wontfix → duplicate |
---|
I suppose this makes this a duplicate of #30032.
Field.db_default
supports expressions, but I don't thinkField.default
does. At least, it's not documented.