Ancestors

Written by valberg on 2024-08-29 at 21:36

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

Written by Benjamin Balder Bach on 2024-08-30 at 06:37

@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

Written by valberg on 2024-08-30 at 06:59

@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

Written by Carlton Gibson 🇪🇺 on 2024-08-30 at 07:01

@valberg @benjaoming “Use the form layer”

=> More informations about this toot | More toots from carlton@fosstodon.org

Written by valberg on 2024-08-30 at 07:04

@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

Written by Carlton Gibson 🇪🇺 on 2024-08-30 at 07:07

@valberg @benjaoming So two points there:

  1. You can just ignore the HTML bit. A Form’s primary job is data sanitation. That’s just Django’s answer here.

  1. If you want to call full_clean() before save() you’re welcome to do so.

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

Written by valberg on 2024-08-30 at 07:14

@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

Written by Carlton Gibson 🇪🇺 on 2024-08-30 at 07:18

@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

Written by valberg on 2024-08-30 at 07:26

@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

Toot

Written by Carlton Gibson 🇪🇺 on 2024-08-30 at 07:31

@valberg @benjaoming I use Forms for API validation too 😜

=> More informations about this toot | More toots from carlton@fosstodon.org

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113049802580350198
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
320.083886 milliseconds
Gemini-to-HTML Time
3.507662 milliseconds

This content has been proxied by September (ba2dc).