Opened 7 days ago

Closed 5 days ago

Last modified 17 hours ago

#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 Clifford Gama, 6 days ago

Has patch: set

comment:2 by Tim Graham, 6 days ago

Field.db_default supports expressions, but I don't think Field.default does. At least, it's not documented.

comment:3 by Jacob Walls, 5 days ago

Cc: Sage Abdullah added
Keywords: expression default added
Resolution: wontfix
Status: assignedclosed

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 Clifford Gama, 17 hours ago

Resolution: wontfixduplicate

I suppose this makes this a duplicate of #30032.

Note: See TracTickets for help on using tickets.
Back to Top