On Thu, Apr 16, 2009 at 5:25 PM, koranthala <koranth...@gmail.com> wrote:
> > On Apr 16, 6:41 pm, Alex Gaynor <alex.gay...@gmail.com> wrote: > > On Thu, Apr 16, 2009 at 6:39 AM, koranthala <koranth...@gmail.com> > wrote: > > > > > Hi, > > > I was comparing between signal and save() and came across this > > > following mail chain - > > > > >http://groups.google.com/group/django-users/browse_thread/thread/d0ff. > .. > > > > > In this it is mentioned that - > > > --- > > > If you want to implement some > > > functionality that operates across multiple types of models, you need > > > to > > > use the signal infrastructure, since the same signal is raised no > > > matter > > > what the type of model (you can differentiate the model type in the > > > signal handler, though). > > > ---- > > > > > I was unable to understand why we should go for signals in case we > > > have to work across models? > > > Say, for example - I have ModelA and ModelB. Everytime, I save an > > > element in ModelA, I need to update 15 fields in ModelB. > > > In this case, shouldn't I do everything in ModelA.save itself? > > > ModelA.save(): > > > super.save() > > > Update 15 fields in ModelB > > > > > I do not understand why we should go for signals in this case too? > > > Sorry if I am dense :-) > > > > One of the simplest reasons to use signals in that situation is because > you > > can't change the code in that application. Say you want to trigger > > something on the User object being saved, you don't want to monkey patch > > django.contrib.auth so use just use the signal for code clarity. > > > > Alex > > > > -- > > "I disapprove of what you say, but I will defend to the death your right > to > > say it." --Voltaire > > "The people's good is the highest law."--Cicero > > Thank you Alex. > But considering the scenario mentioned above - wherein I need to make > modifications to a separate table - both of which I have full access > to - should I go ahead with signals or should I go with save? > Malcolms mail - the link mentioned earlier - seems to suggest that I > should be using signals - but I am unable to understand the > justification for it. > > If ModelA and ModelB belong (logically) together, I'd go for save(). Say ModelA is "Article" and ModelB is "Category" and when you insert an article, you want to update some field in the corresponding category, I'd put that into the article's save() method. If the two models are not really related to each other, I'd use the signal. E.g. ModelA is "Article" and ModelB is "LogEntry". The logs are a different set of functionality, so this should not be mixed up. Furthermore, you probably want to use the log functionality with other models, too. So you could just listen for the signal(s) to apply it to all of them. I think, the latter is what Malcolm meant with "implement some functionality that operates across multiple types of models". --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---