Notes on using the post_save signal 1 - Do not use post_save to save the instance that is passed. The `instance.save` is going to cause a never ending loop. 2 - Use post_save signal to do things not related to updating the model being saved. Like updating a related model. 3 - Override the save method of the model to do any changes you need to do to override the default save. 4 - If you have to use signals in this case (Eg. You cannot edit the models because they are part of a different library) use the pre_save signal and just modify the attributes without calling `instance.save()`
On Tue, Jul 8, 2014 at 3:30 PM, <bangpypers-requ...@python.org> wrote: > Send BangPypers mailing list submissions to > bangpypers@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/bangpypers > or, via email, send a message with subject or body 'help' to > bangpypers-requ...@python.org > > You can reach the person managing the list at > bangpypers-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of BangPypers digest..." > > > Today's Topics: > > 1. Anyone Travelling for EuroPython ? (konark modi) > 2. Django - Infinte Loop (Anand Reddy Pandikunta) > 3. Re: Django - Infinte Loop (kracekumar ramaraju) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 7 Jul 2014 21:49:07 +0530 > From: konark modi <modi.kon...@gmail.com> > To: Bangalore Python Users Group - India <bangpypers@python.org> > Subject: [BangPypers] Anyone Travelling for EuroPython ? > Message-ID: > < > caphs5j_9hrf-javt+uhbca8hfxaojk8syrzl__uxaa04q7y...@mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > Hi > > Wanted to know if anyone on the list is planning to travel for EuroPython > 2014 this July. > > Regards > Konark > > > ------------------------------ > > Message: 2 > Date: Tue, 8 Jul 2014 13:13:02 +0530 > From: Anand Reddy Pandikunta <anand21na...@gmail.com> > To: Bangalore Python Users Group - India <bangpypers@python.org> > Subject: [BangPypers] Django - Infinte Loop > Message-ID: > <CALCtiO56Lk=yB7003L20aku9UXn8b_FAUQcrdo= > nhofnlvq...@mail.gmail.com> > Content-Type: text/plain; charset=ISO-8859-1 > > 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 > > > ------------------------------ > > Message: 3 > Date: Tue, 8 Jul 2014 13:27:09 +0530 > From: kracekumar ramaraju <kracethekingma...@gmail.com> > To: Bangalore Python Users Group - India <bangpypers@python.org> > Subject: Re: [BangPypers] Django - Infinte Loop > Message-ID: > <CAO5c=mk0v9nyGRVdtVfhTz68=PLfNnif7nKdh= > u96wvfwp6...@mail.gmail.com> > Content-Type: text/plain; charset=UTF-8 > > 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 < > anand21na...@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@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>* > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > https://mail.python.org/mailman/listinfo/bangpypers > > > ------------------------------ > > End of BangPypers Digest, Vol 83, Issue 3 > ***************************************** > _______________________________________________ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers