I'm working in a pre_save signal of a model and I don't know how to check if the record is a INSERT or UPDATE. Code below (doesn't work properly):
@receiver(pre_save, sender=Person)
def pre_save_person(sender, instance, **kwargs):
if not instance.pk:
print 'INSERT !!!!!!'
else:
print 'UPDATE !!!!!!'
Can you help me? The project uses django 1.8.7.
Thanks for any help
Following @ knbk comment, the instance.pk will always exist at this point. You must check if this instance.pk exists in database:
@receiver(pre_save, sender=Person)
def pre_save_person(sender, instance, **kwargs):
num = Person.objects.filter(pk=instance.pk).count()
if num == 0 :
print 'INSERT !!'
else:
print 'UPDATE !!'