#35575: Model.full_clean() does not recalculate GeneratedFields prior to validating
model constraints
-------------------------------------+-------------------------------------
Reporter: Mark | Owner: Mark Gensler
Gensler |
Type: Bug | Status: assigned
Component: Database | Version: 5.0
layer (models, ORM) | Keywords: generatedfield
Severity: Normal | uniqueconstraint checkconstraint
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
`GeneratedField`s on a model instance are not recalculated during
`Model.full_clean()`. Therefore, if a `GeneratedField` is included in the
`*expressions` or `fields` parameters of a `UniqueConstraint` or
`CheckConstraint` in the instance's `Model._meta`, the values of the
`GeneratedField` attribute are not recalculated prior to calling
`BaseConstraint.validate()`.
Instead, the model instance's `GeneratedField` attribute will return the
value which corresponds to the most recent refresh of the instance from
the database. If the instance is new and has not yet been saved, the
following error is raised:
`AttributeError: Cannot read a generated field from an unsaved model.`
An `IntegrityError` is correctly raised when calling `Model.save()` if any
constraints involving `GeneratedField`s are violated.
Instead, the `GeneratedField`s should have their values recalculated as
part of `Model.full_clean()`.
This ticket was raised after fixing the bug in #35560 with PR
[
https://github.com/django/django/pull/18309].
--
Ticket URL: <
https://code.djangoproject.com/ticket/35575>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.