Created
September 6, 2013 10:52
-
-
Save gterzian/6462324 to your computer and use it in GitHub Desktop.
Django Recipes: Manipulating a model’s m2m from within a signal handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db.models.signals import pre_save, post_save, m2m_changed | |
def save_handler(sender, instance, *args, **kwargs): | |
m2m_changed.connect(m2m_handler, sender=sender.m2mfield.through, weak=False) | |
def m2m_handler(sender, instance, action, *args, **kwargs): | |
if action =='post_clear': | |
succesfully_manipulate_m2m(instance) | |
pre_save.connect(save_handler, sender=YouModel, weak=False) |
problem:
When you manipulate the m2m of a model within a post or pre_save signal receiver, your changes get wiped out in the subsequent ‘clearing’ of the m2m by Django.
solution:
In you post or pre_save signal handler, register another handler to the m2m_changed signal on the m2m intermediary model of the model whose m2m you want to update.
Please note that this second handler will receive several m2m_changed signals, and it is key to test for the value of the ‘action’ arguments passed along with them.
Within this second handler, check for the ‘post_clear’ action. When you receive a signal with the post_clear action, the m2m has been cleared by Django and you have a chance to successfully manipulate it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://polyglotfactotum.wordpress.com/2013/09/06/django-recipes-manipulating-a-models-m2m-from-within-a-signal-handler/