On Mon, 2009-02-16 at 22:38 -0800, gganesh wrote:
> hi,
> i wrote
> def after_save(sender,instance,created,**kaw):
>       instance.acct_number ='RBSB' + str(instance.id)
>       instance.save()
> 
> 
> post_save.connect(after_save,sender=Selector)
> 
> 
> once when i save it gives out error like
>         "maximum recursion depth exceeded in cmp "Exception

That's not surprising. When you see that error, it's usually a hint that
you've got an infinite loop (the other possibility is that you're doing
a really complex computation, but that's relatively rare, particularly
in web programming), so start looking for what it can be. In this case,

(1) Something calls Selector.save()
(2) save() finished and post_save signal handler is called.
(3) your signal handler calls Selector.save() again
(4) save() calls the post_save signal handler (again)
(5) Go back to step 3, repeat forever.

You're going to have to build something into your signal handler to
detect the loop. One idea might be to add an attribute indicating that
you are already updating it:

        def after_save(sender,instance,created,**kaw):
           if hasattr(instance, '_already_saving):
              del instance._already_saving
              return
           instance._already_saving = True
           instance.acct_number ='RBSB' + str(instance.id)
           instance.save()
        
However, in this particular case, it seems like using a post_save signal
handler might be overkill. Can you override the Selector.save() method
instead?

Signals are useful, generally, when you want to do something to multiple
models. In this case, the behaviour seems very specific to one
particular model, so just doing it in the model's save method seems more
sensible.

The one exception to this guideline is if you don't "own" the Selector
source -- if it's a third-party model -- and so can't modify the save()
method. In that case, a post_save handler can be a way to add new
behaviour. But that's more of a last resort situation.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to