How about a new class, `ValidatedModel`, that subclasses `Model` and does 
nothing more than call `full_clean()` on `save()`?

This would be completely backwards compatible, would clearly communicate 
what it does, and when documented right next to `Model` make it fairly 
obvious that Model is something other than validated, hopefully preventing 
many footguns.

Or, and I think this would be better, if the current Model were renamed 
`UnvalidatedModel`, the new validated implementation above were `Model`. 
This upgrade path is a simple string replacement for those legacy codebases 
(Model->UnvalidatedModel), making it abundantly clear they are not 
validated, and new apps following the most naive path (Model) are as safe 
as possible. The new, validated, `Model.save()` could accept the kwarg 
`validate=False` as an opt-out, which as much as I hate to admit it is an 
important option for some codebases.

On Thursday, September 29, 2022 at 5:19:07 PM UTC-7 cur...@tinbrain.net 
wrote:

> On Thu, 29 Sep 2022, at 14:29, Aaron Smith wrote:
>
> Why doesn't Django validate Models on save()?
>
>
> The short answer is: backwards compatibility.
>
> Model level validation hasn't always been a thing, with Django initially 
> depending primarily on Form validation.
>
> Since it hadn't _always_ been there, it wasn't possible to introduce it, 
> enforce it, and not break most apps out there.
>
> There was so much code written that generally assumed it could call 
> `save()` and not have to catch validation errors.
>
> For what it's worth, I'm all in favor of making it run on `save()` ... 
> updating the documentation and preparing the community is going to be a 
> mammoth task, however. A safe course through this will take some very 
> careful planning.
>
> --
> C
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/f4224b99-6207-4916-a203-d8596f7f9211n%40googlegroups.com.

Reply via email to