Django 1.8 post_save without recursion

494 views
Skip to first unread message

Neto

unread,
Mar 13, 2015, 1:35:46 AM3/13/15
to django...@googlegroups.com
What is the best way to avoid recursion when using post_save?

@receiver(post_save, sender=Person)
def do(sender, instance, *args, **kwargs):
instance.ok = True
instance.save()

Simon Charette

unread,
Mar 13, 2015, 11:47:52 AM3/13/15
to django...@googlegroups.com
I don't think there's best a way of doing it but in your specific case the following should do:

@receiver(post_save, sender=Person)
def do(instance, update_fields, *args, **kwargs):
   
if update_fields == {'ok'}:
       
return
    instance
.ok = True
    instance
.save(update_fields={'ok'})

Simon

Alexander MacQueen

unread,
Mar 19, 2015, 11:29:42 AM3/19/15
to django...@googlegroups.com
The post_save 'created' argument could help if you only wanted to trigger the signal when the Person was first created:

@receiver(post_save, sender=Person)
def do(instance, *args, **kwargs):
   
if kwargs.get('created'):
       
instance.ok = True
       
instance.save()
   

Reply all
Reply to author
Forward
0 new messages