Django save_m2m() in a separate, delayed transaction

I'm facing a vexing issue in which two adjacent lines of code seem to be committed separately to the database- with one of them hanging out for a full day or more before getting committed.

        if form.is_valid():
            instance = form.save(commit=False)

            if form.cleaned_data['assigned_to_mtm'].count() <= 0:
                instance.status = "Unassigned"
            elif instance.status == 'Unassigned':
                instance.status = "Assigned"

            instance.save()
            form.save_m2m()

With the above, I am finding the assigned_to_mtm reflected right away in the database. The status, however, isn't. Instead, when a separate save (even happening the next day) updates the status to something else, then this saved status comes in right afterward with the same timestamp, reverting its value to what it should have been at the time the mtm was saved.

I'm astonished that I can't get these two to just happen in a single transaction (I tried the @transaction.non_atomic_requests decorator and doing it myself- that didn't help), let alone that the instance.save() hangs out uncommitted for so long, then gets committed only when the status is updated again separately.

I might not even care, if the order of saves was at least preserved, but confoundingly, my earlier save is always committed after the next save.

What am I doing wrong here? Can someone provide insight into why this happens?

Back to Top