Django: How to access original (unmodified) instance in post_save signal

I believe post_save is too late to retrieve the unmodified version. As the name implies the data has already been written to the db at that point. You should use pre_save instead. In that case you can retrieve the model from the db via pk: old = Vote.objects.get(pk=instance.pk) and check for differences in the current … Read more

How do I mock a django signal handler?

Possibly a better idea is to mock out the functionality inside the signal handler rather than the handler itself. Using the OP’s code: @receiver(post_save, sender=User, dispatch_uid=’myfile.signal_handler_post_save_user’) def signal_handler_post_save_user(sender, *args, **kwargs): do_stuff() # <– mock this def do_stuff(): … do stuff in here Then mock do_stuff: with mock.patch(‘myapp.myfile.do_stuff’) as mocked_handler: self.assert_equal(mocked_handler.call_count, 1)

How do I prevent fixtures from conflicting with django post_save signal code?

I think I figured out a way to do this. There is a ‘raw’ parameter in the kwargs passed in along with signals so I can replace my test above with this one: if (kwargs.get(‘created’, True) and not kwargs.get(‘raw’, False)): Raw is used when loaddata is running. This seems to do the trick. It is … Read more

Why Django model signals are not working?

Seems like your post_save.connect is not executed. You should import signals somewhere. For django 1.7 it is recommended to do this in the app’s config ready() function. Read the “Where should this code live?” side note in the docs. For example if your app is called activity: activity/__init__.py default_app_config = ‘activity.apps.ActivityAppConfig’ activity/apps.py from django.apps import … Read more

What are the options for overriding Django’s cascading delete behaviour?

Just a note for those who run into this issue as well, there is now an built-in solution in Django 1.3. See the details in the documentation django.db.models.ForeignKey.on_delete Thanks for editor of Fragments of Code site to point it out. The simplest possible scenario just add in your model FK field definition: on_delete=models.SET_NULL

Identify the changed fields in django post_save signal

If you want to compare state before and after save action, you can use pre_save signal which provide you instance as it should become after database update and in pre_save you can read current state of instance in database and perform some actions based on difference. from django.db.models.signals import pre_save from django.dispatch import receiver @receiver(pre_save, … Read more

django – comparing old and new field value before saving

There is very simple django way for doing it. “Memorise” the values in model init like this: def __init__(self, *args, **kwargs): super(MyClass, self).__init__(*args, **kwargs) self.initial_parametername = self.parametername — self.initial_parameternameX = self.parameternameX Real life example: At class: def __init__(self, *args, **kwargs): super(MyClass, self).__init__(*args, **kwargs) self.__important_fields = [‘target_type’, ‘target_id’, ‘target_object’, ‘number’, ‘chain’, ‘expiration_date’] for field in self.__important_fields: … Read more

Django post_save() signal implementation

If you really want to use signals to achieve this, here’s briefly how, from django.db.models.signals import post_save from django.dispatch import receiver class TransactionDetail(models.Model): product = models.ForeignKey(Product) # method for updating @receiver(post_save, sender=TransactionDetail, dispatch_uid=”update_stock_count”) def update_stock(sender, instance, **kwargs): instance.product.stock -= instance.amount instance.product.save()

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)