Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Signals: using update_field as condition

Can someone help me understand the update_field argument for Django signals?

According to the docs:

update_fields: The set of fields to update explicitly specified in the save() method. None if this argument was not used in the save() call.

I'm not clear on what this means. I was trying to use it to prevent a signal function from executing unless certain fields were updated:

@receiver(post_save, sender=SalesRecord)
def spawn_SaleSource_record(sender, update_fields, created, instance, **kwargs):
    if created or update_fields is 'sale_item' or 'sales_qty':
        *do function*

However, it seems that it still executes during another signal process when an object is saved, even if an unspecified field is explicitly updated:

x = SalesRecord.objects.filter(paid_off=False, customer=instance.customer).first()
x.paid_off = True
x.save(update_fields=['paid_off'])

Am I going about this wrong?

like image 653
Adam Starrh Avatar asked Jul 18 '15 23:07

Adam Starrh


1 Answers

Your condition does not correspond to what you want as 'sales_qty' is always true.

You want your condition to be:

if created or 'sale_item' in update_fields or 'sales_qty' in update_fields:
like image 57
Gabriel Pichot Avatar answered Oct 08 '22 13:10

Gabriel Pichot