Monday, April 5, 2021

Re: calling self.clean() before super.save()

Thank you Ryan. I'm relatively sure that I only raise exceptions in clean() everywhere. But I will double-check to be sure to be sure.

Cheers

Mike

--
(Unsigned mail from my phone)



-------- Original message --------
From: Ryan Nowakowski <tubaman@fattuba.com>
Date: 6/4/21 00:23 (GMT+10:00)
To: django-users@googlegroups.com
Subject: Re: calling self.clean() before super.save()

I believe ModelForm calls your model's self.clean so if you use it, or use the admin which uses it, you'll need to make sure that clean is idempotent since it'll get called at least twice in that case.

On April 4, 2021 6:33:54 PM CDT, Mike Dewhirst <miked@dewhirst.com.au> wrote:
Is it sensible to generally call self.clean() in a model's save() method 
prior to calling super().save()

Until now I have been calling clean() deliberately prior to calling
save() but I have a lot of models where save() pre-processes data just
prior to calling the model's super().save(*args, **kwargs)

I feel as though it would more robust to pre-process the data, then call
self.clean() and finally make the super call.

Thanks for any advice.

Cheers

Mike

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CBAFE56E-1466-4538-A4D2-20B90D088366%40fattuba.com.

No comments:

Post a Comment