[BangPypers] Django - Infinte Loop
Navin Kabra
navin.kabra at gmail.com
Tue Jul 8 12:15:03 CEST 2014
1. Why are you doing this using a signal? Signals are best used when
saving of Model1 needs to trigger some action on Model2. If you want to
modify Model1 itself, you're better off doing this inside MyModel::save
2. If you really want to do it this way, put an if condition in my_func
so that instance.save() is called only the first time. It is hard to
give details without knowing the reason why you're doing this. But
something like this would work:
def my_func(sender, instance, created, **kwargs):
if instance.status == 'task completed':
return
# do something
instance.status = 'task completed'
instance.save()
Or even something like this:
def my_func(sender, instance, created, **kwargs):
try:
if instance.in_signal:
return
except AttributeError:
instance.in_signal = True
# do something
instance.status = 'task completed'
instance.save()
Anand Reddy Pandikunta <anand21nanda at gmail.com> writes:
> Hi,
>
> *models.py*
>
> *def my_func(sender, instance, created, **kwargs):*
> * # do something*
> * instance.status = 'task completed'*
> * instance.save()*
>
> *class MyModel(models.Model):*
> * status = models.CharField(max_length=100, blank=True)*
> * # some code*
>
> *signals.post_save.connect(my_func, sender=MyModel)*
>
>
> I am using post_save signal to connect to a function.
>
> If a new instance of model is saved, post_save signal connects to my_func.
> Once the function is executed, I am updating status of the model.
> This is again sending post_save signal which is leading to infinite loop.
>
> I want to execute my_func only once and update status many times.
> Does any one know how to do this?
>
>
> --
> - Anand Reddy Pandikunta
> www.avilpage.com
> www.quotes160.com
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
More information about the BangPypers
mailing list