Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is <strong><em>cleared</em></strong> and the new values set to whatever came out of the form. So if you are in the save() of the main object you are in a window of opportunity where the M2M hasn't been updated yet. In fact, if you <em>try</em> to do something to the M2M, the change will get wiped out by the clear(). I ran into this about a year ago.</p> <p>The code has changed somewhat from the pre-ORM refactor days, but it boils down to code in <code>django.db.models.fields.ManyRelatedObjectsDescriptor</code> and <code>ReverseManyRelatedObjectsDescriptor</code>. Look at their __set__() methods and you'll see <code>manager.clear(); manager.add(*value)</code> That clear() complete cleans out any M2M references for the current main object in that table. The add() then sets the new values.</p> <p>So to answer your question: yes, this is a transaction issue.</p> <p>Is there a signal thrown when the transaction ends? Nothing official, but read on:</p> <p>There was a <a href="https://stackoverflow.com/questions/950214/run-code-after-transaction-commit-in-django">related thread a few months ago</a> and MonkeyPatching was one method proposed. <a href="https://stackoverflow.com/questions/950214/run-code-after-transaction-commit-in-django/1837402#1837402">Grégoire posted a MonkeyPatch</a> for this. I haven't tried it, but it looks like it should work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload