[BangPypers] Django - Infinte Loop

kracekumar ramaraju kracethekingmaker at gmail.com
Tue Jul 8 09:57:09 CEST 2014


Hi Anand

 I would save *don't* use Django signal. Signals are hard to test and don't
know when they will be executed.

Suggestions
- If post_save function does some work like updating external service which
can take data, use rq or celery.
- You can override django save.

class MyModel(BaseClass):

    def save(self, **kwargs)
       # set all attributes
      # call super class save
      # call function here - blocking call.





On Tue, Jul 8, 2014 at 1:13 PM, Anand Reddy Pandikunta <
anand21nanda at gmail.com> wrote:

> 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
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com <http://kracekumar.com>*


More information about the BangPypers mailing list