[Django] #34061: Model validations should be run on save()

3 views
Skip to first unread message

Django

unread,
Sep 28, 2022, 8:39:10 PM9/28/22
to django-...@googlegroups.com
#34061: Model validations should be run on save()
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
threewordphrase |
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.0
layer (models, ORM) | Keywords: model, validation,
Severity: Normal | validations
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Most modern Django applications, at least as far as I have seen in 6 years
of mostly working with Django, are headless API apps that do not use
Forms. As a matter of fact, I have never used Forms in the several dozen
django apps I've worked on, including several mission-critical APIs at
multi-hundred-person companies.

Not running validations by default is a **serious** footgun that has
ruined more than one developer's day. Yes, it's in the documentation. But
the broad expectation is that a web framework will do this sort of thing
by default without extra configuration or overrides. Model validations
should function as expected ''by the naive'' regardless of how the model
is saved.

Ideally the Model class should call `full_clean()` in it's save method. If
this can't be done for backwards compatibility (which is a silly reason,
but I digress) I propose we add a new `ValidatedModel` class that
subclasses `Model`. There should be a clear and obvious option to fulfill
the basic and reasonable expectations of the new Django developer.

I'm more than happy to make this PR.

--
Ticket URL: <https://code.djangoproject.com/ticket/34061>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 28, 2022, 9:39:19 PM9/28/22
to django-...@googlegroups.com
#34061: Model validations should be run on save()
-------------------------------------+-------------------------------------
Reporter: Aaron Smith | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: model, validation, | Triage Stage:
validations | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Aaron Smith:

Old description:

> Most modern Django applications, at least as far as I have seen in 6
> years of mostly working with Django, are headless API apps that do not
> use Forms. As a matter of fact, I have never used Forms in the several
> dozen django apps I've worked on, including several mission-critical APIs
> at multi-hundred-person companies.
>
> Not running validations by default is a **serious** footgun that has
> ruined more than one developer's day. Yes, it's in the documentation. But
> the broad expectation is that a web framework will do this sort of thing
> by default without extra configuration or overrides. Model validations
> should function as expected ''by the naive'' regardless of how the model
> is saved.
>
> Ideally the Model class should call `full_clean()` in it's save method.
> If this can't be done for backwards compatibility (which is a silly
> reason, but I digress) I propose we add a new `ValidatedModel` class that
> subclasses `Model`. There should be a clear and obvious option to fulfill
> the basic and reasonable expectations of the new Django developer.
>
> I'm more than happy to make this PR.

New description:

Most modern Django applications, at least as far as I have seen in 6 years
of mostly working with Django, are headless API apps that do not use
Forms. As a matter of fact, I have never used Forms in the several dozen
django apps I've worked on, including several mission-critical APIs at
multi-hundred-person companies.

Not running validations by default is a **serious** footgun that has
ruined more than one developer's day. Yes, it's in the documentation. But
the broad expectation is that a web framework will do this sort of thing
by default without extra configuration or overrides. Model validations
should function as expected ''by the naive'' regardless of how the model
is saved.

Ideally the Model class should call `full_clean()` in it's save method. If

this can't be done for backwards compatibility, I propose we add a new


`ValidatedModel` class that subclasses `Model`. There should be a clear
and obvious option to fulfill the basic and reasonable expectations of the
new Django developer.

I'm more than happy to make this PR.

--

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

Django

unread,
Sep 28, 2022, 11:55:18 PM9/28/22
to django-...@googlegroups.com
#34061: Model validations should be run on save()
-------------------------------------+-------------------------------------
Reporter: Aaron Smith | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: model, validation, | Triage Stage:
validations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Uncategorized => New feature
* resolution: => wontfix


Comment:

This behavior is by design and it's
[https://docs.djangoproject.com/en/dev/ref/models/instances/#validating-
objects documented] (as you've already noticed). If you want to change
design decisions then please first start a discussion on the
DevelopersMailingList, where you'll reach a wider audience and see what
other think, and
[https://docs.djangoproject.com/en/stable/internals/contributing/bugs-and-
features/#requesting-features follow the guidelines with regards to
requesting features]. We must reach a strong consensus in the discussion
to move it forward.

> I propose we add a new `ValidatedModel` class that subclasses `Model`.

You can always add and use your own subclass in the code.

--
Ticket URL: <https://code.djangoproject.com/ticket/34061#comment:2>

Reply all
Reply to author
Forward
0 new messages