So, validating and saving models instances in #Django. Shouldn't we just call Model.full_clean()
from Model.save()
as a default, and make it possible to skip with a skip_clean
argument? Am I missing something? It feels like a big win and avoids the footgun aspect of forgetting to run full_clean
.
=> More informations about this toot | More toots from valberg@social.data.coop
@valberg it's a pretty heavy subject. But I think that default would be too much. Then you would create a new footgun that makes you code perform badly.. or unable to save things that need to be saved.
https://docs.djangoproject.com/en/5.1/ref/models/instances/#validating-objects
=> More informations about this toot | More toots from benjaoming@social.data.coop
@benjaoming I'm willing to pay a bit of overhead if it means that data being saved to the database has been validated. And in those instances where performance is of utter importance I'm OK with having to do optimisations.
=> More informations about this toot | More toots from valberg@social.data.coop
@valberg @benjaoming “Use the form layer”
=> More informations about this toot | More toots from carlton@fosstodon.org
@carlton @benjaoming If you remove all the "render this into a HTML form" stuff of "the form layer" and make it into a clean "validation layer", I would be inclined to agree. But then again, it is a lot of overhead for just calling .full_clean()
followed by .save()
.
=> More informations about this toot | More toots from valberg@social.data.coop
@valberg @benjaoming So two points there:
The was an oldish thread on django-developers that went into all the back story here at length, if you like archeology!
=> More informations about this toot | More toots from carlton@fosstodon.org
@carlton @benjaoming Hmm, yeah I can try to ignore it, but somehow it is hard since I've learned to use forms first and foremost as a way to validate input from a HTML form. And the documentation talks about forms in that way.
Also, having to activate the form layer when I have clean methods right there on my model feels tedious.
I know I'm welcome to call it, the thing is that I (and other Django devs I know) forget. So having it as a default still makes sense to me.
=> More informations about this toot | More toots from valberg@social.data.coop
@valberg @benjaoming Yes! And that feeling has been had by lots of people over the years. So you’re not “wrong” — but that’s not how Django does it…
“…Validate data from an HTML form…” — that’s Django’s primary use-case. Django is a web framework after all 🥳
I’m old. I will throw EVERYTHING through a form (or formset). I want to know that it’s been properly validated, even if it’s supposedly “trusted” — Uh huh, that CVS is valid you say?
=> More informations about this toot | More toots from carlton@fosstodon.org
@carlton @benjaoming But it could become how Django does it if it makes sense 😀
Yeah touché 😆 I just wish the validation part was separated so I could use that for my APIs and the HTML parts for my HTML views.
Do you then never add validation logic to the .clean() method of Model? Where is the locality of behaviour in all of this 😜
=> More informations about this toot | More toots from valberg@social.data.coop
@valberg @benjaoming I use Forms for API validation too 😜
=> More informations about this toot | More toots from carlton@fosstodon.org
text/gemini
This content has been proxied by September (ba2dc).