django-signals

Identify the changed fields in django post_save signal

Identify the changed fields in django post_save signal Question: I’m using django’s post_save signal to execute some statements after saving the model. class Mode(models.Model): name = models.CharField(max_length=5) mode = models.BooleanField() from django.db.models.signals import post_save from django.dispatch import receiver @receiver(post_save, sender=Mode) def post_save(sender, instance, created, **kwargs): # do some stuff pass Now I want to execute …

Total answers: 8

Can I manually trigger signals in Django?

Can I manually trigger signals in Django? Question: I’ve written some signals in my Django app that are supposed to send out an email when a particular model instance is created or modified, but the signal receiver function doesn’t seem to be responding; at any rate, I’m not getting any emails through (although I have …

Total answers: 1

django – comparing old and new field value before saving

django – comparing old and new field value before saving Question: I have a django model, and I need to compare old and new values of field BEFORE saving. I’ve tried the save() inheritance, and pre_save signal. It was triggered correctly, but I can’t find the list of actually changed fields and can’t compare old …

Total answers: 11

TransactionManagementError "You can't execute queries until the end of the 'atomic' block" while using signals, but only during Unit Testing

TransactionManagementError "You can't execute queries until the end of the 'atomic' block" while using signals, but only during Unit Testing Question: I am getting TransactionManagementError when trying to save a Django User model instance and in its post_save signal, I’m saving some models that have the user as the foreign key. The context and error …

Total answers: 11

when to use pre_save, save, post_save in django?

when to use pre_save, save, post_save in django? Question: I see I can override or define pre_save, save, post_save to do what I want when a model instance gets saved. Which one is preferred in which situation and why? Asked By: eugene || Source Answers: pre_save it’s used before the transaction saves. post_save it’s used …

Total answers: 3

How do I mock a django signal handler?

How do I mock a django signal handler? Question: I have a signal_handler connected through a decorator, something like this very simple one: @receiver(post_save, sender=User, dispatch_uid=’myfile.signal_handler_post_save_user’) def signal_handler_post_save_user(sender, *args, **kwargs): # do stuff What I want to do is to mock it with the mock library http://www.voidspace.org.uk/python/mock/ in a test, to check how many times …

Total answers: 7

Django post_save() signal implementation

Django post_save() signal implementation Question: I have a question about django. I have ManyToMany Models here class Product(models.Model): name = models.CharField(max_length=255) price = models.DecimalField(default=0.0, max_digits=9, decimal_places=2) stock = models.IntegerField(default=0) def __unicode__(self): return self.name class Cart(models.Model): customer = models.ForeignKey(Customer) products = models.ManyToManyField(Product, through=’TransactionDetail’) t_date = models.DateField(default=datetime.now()) t_sum = models.FloatField(default=0.0) def __unicode__(self): return str(self.id) class TransactionDetail(models.Model): product …

Total answers: 5

Django post_save preventing recursion without overriding model save()

Django post_save preventing recursion without overriding model save() Question: There are many Stack Overflow posts about recursion using the post_save signal, to which the comments and answers are overwhelmingly: “why not override save()” or a save that is only fired upon created == True. Well I believe there’s a good case for not using save() …

Total answers: 11

How to use Django model inheritance with signals?

How to use Django model inheritance with signals? Question: I have a few model inheritance levels in Django: class WorkAttachment(models.Model): “”” Abstract class that holds all fields that are required in each attachment “”” work = models.ForeignKey(Work) added = models.DateTimeField(default=datetime.datetime.now) views = models.IntegerField(default=0) class Meta: abstract = True class WorkAttachmentFileBased(WorkAttachment): “”” Another base class, but …

Total answers: 9

django post_save signals on update

django post_save signals on update Question: I am trying to set up some post_save receivers similar to the following: @receiver(post_save, sender=Game, dispatch_uid=’game_updated’) def game_updated(sender, **kwargs): ”’DO SOME STUFF HERE”’ MyPick.objects.filter(week=game.week, team=game.home_team).update(result=home_result) MyPick.objects.filter(week=game.week, team=game.away_team).update(result=away_result) @receiver(post_save, sender=MyPick, dispatch_uid=’user_pick_updated’) def update_standings(sender, **kwargs): ”’DO STUFF”’ The first receiver is getting called correctly after an update on the Game object, …

Total answers: 2