Sanity check please

30 views
Skip to first unread message

Mike Dewhirst

unread,
Oct 23, 2022, 3:53:22 AM10/23/22
to Django users
In the Django Admin I have a model central to a bunch of FKs and M:Ms plus a lot of processing on saving.

This is my solution which seems to work but frightens me a bit.

class Chemical(models.Model):
    # lots of fields
    def save(self, *args, **kwargs):
        self.process_stuff_based_on_field_values_pre_save()
        first = False
        if not self.id:
            first = True
            super().save(force_insert=True, *args, **kwargs)
        if self.id:
            self.create_or_update_a_bunch_of_related_records(first=first)
        super().save(force_insert=False, *args, **kwargs)
        self.create_or_update_a_bunch_of_m2m_records(first=first)
    def process_stuff_based_on_field_values_pre_save(self, first=False):
        ...



    def create_or_update_a_bunch_of_m2m_records(self, first=False):
        ...


    # lots of other methods

    Thanks for any warnings, caveats

Cheers

Mike

-- 
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.
OpenPGP_signature

Ryan Nowakowski

unread,
Oct 23, 2022, 8:55:56 AM10/23/22
to django...@googlegroups.com
Do you need to guarantee that any of this is atomic? If so then I usually wrap this kind of code in a "with transaction.atomic()".

If not, is all this slowing down your UX? If so, then you might consider doing some of this in an asynchronous task after the save using celery or something like it.  If not, then what you have is fine.

Mike Dewhirst

unread,
Oct 23, 2022, 5:54:52 PM10/23/22
to django...@googlegroups.com, Ryan Nowakowski
On 23/10/2022 11:55 pm, Ryan Nowakowski wrote:
Do you need to guarantee that any of this is atomic? If so then I usually wrap this kind of code in a "with transaction.atomic()".

If not, is all this slowing down your UX? If so, then you might consider doing some of this in an asynchronous task after the save using celery or something like it.  If not, then what you have is fine.

Thanks Ryan. For other reasons I'll definitely look at async.

It still seems to work fine in real life but my unit tests take exception to force_insert being a KeyError when *args, **kwargs are included in the super calls. I have now removed those *args, **kwargs with no apparent ill effects.

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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/D30805E6-C2F6-4878-829E-CC39A2D15C6B%40fattuba.com.
OpenPGP_signature

Ryan Nowakowski

unread,
Oct 23, 2022, 10:00:58 PM10/23/22
to Mike Dewhirst, django...@googlegroups.com


On October 23, 2022 4:54:00 PM CDT, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
>On 23/10/2022 11:55 pm, Ryan Nowakowski wrote:
>It still seems to work fine in real life but my unit tests take exception to force_insert being a KeyError when *args, **kwargs are included in the super calls. I have now removed those *args, **kwargs with no apparent ill effects.

Yup, I missed that when reading your code below. Typically if your specify a keyword arg like force_insert, it needs to be after *args.
>> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/D30805E6-C2F6-4878-829E-CC39A2D15C6B%40fattuba.com <https://groups.google.com/d/msgid/django-users/D30805E6-C2F6-4878-829E-CC39A2D15C6B%40fattuba.com?utm_medium=email&utm_source=footer>.
>
>

Mike Dewhirst

unread,
Oct 24, 2022, 12:03:28 AM10/24/22
to django...@googlegroups.com, Ryan Nowakowski
On 24/10/2022 12:59 pm, Ryan Nowakowski wrote:

On October 23, 2022 4:54:00 PM CDT, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
On 23/10/2022 11:55 pm, Ryan Nowakowski wrote:
It still seems to work fine in real life but my unit tests take exception to force_insert being a KeyError when *args, **kwargs are included in the super calls. I have now removed those *args, **kwargs with no apparent ill effects.
Yup, I missed that when reading your code below. Typically if your specify a keyword arg like force_insert, it needs to be after *args.

That *is* interesting. I'll have to re-study arg sequencing.

Thanks Ryan

M
OpenPGP_signature

Derek

unread,
Oct 24, 2022, 1:09:59 AM10/24/22
to Django users
Link to a discussion on RealPython about args & kwargs:
https://realpython.com/python-kwargs-and-args/#ordering-arguments-in-a-function

Mike Dewhirst

unread,
Oct 24, 2022, 9:28:20 AM10/24/22
to django...@googlegroups.com, Derek
On 24/10/2022 4:09 pm, Derek wrote:
Link to a discussion on RealPython about args & kwargs:
https://realpython.com/python-kwargs-and-args/#ordering-arguments-in-a-function

Thank you Derek. Yes that is how I originally understood it.

Turns out Django doesn't seem to like *args, **kwargs at all when calling

super().save(force_insert=False)  (or True)

Cheers

mike

OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages