Can someone help me understand the update_field
argument for Django signals?
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?
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: