Re: Performing an action on model.save() but not on update

2010-04-26 Thread Nick Serra
:) On Apr 26, 9:48 am, Jim N wrote: > Nick, > > Thanks very much.  That worked!  I can't work out why my code didn't > though (or rather, worked twice). > > -Jim > > On Apr 23, 6:37 pm, Nick Serra wrote: > > > > > > > Try this out and see what happens: > > > def _hook_post_save_answer(instance,

Re: Performing an action on model.save() but not on update

2010-04-26 Thread Jim N
Nick, Thanks very much. That worked! I can't work out why my code didn't though (or rather, worked twice). -Jim On Apr 23, 6:37 pm, Nick Serra wrote: > Try this out and see what happens: > > def _hook_post_save_answer(instance, created, sender, **kwargs): >     if created: >         if instan

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Nick Serra
Try this out and see what happens: def _hook_post_save_answer(instance, created, sender, **kwargs): if created: if instance.user: quser = instance.user.get_profile() quser.increment_answers() instance.question.increment_responses() post_save.connect(_ho

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Jim N
OK, that makes total sense. I've implemented it like so: def _hook_post_save_answer(instance, sender, **kwargs): if 'created' in kwargs and kwargs['created'] == True: if instance.user: quser = instance.user.get_profile() quser.increment_answers() inst

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Nick Serra
I didn't even think of that. It's not very common to be specifying pk's on create anyway, so yours would probably be fine. I just think signals are cleaner and use them when I can :) On Apr 23, 4:47 pm, Skylar Saveland wrote: > Yeah, and I think my suggestion fails for a user-defined rather than

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Skylar Saveland
Yeah, and I think my suggestion fails for a user-defined rather than auto-incrementing pk. On Apr 23, 4:21 pm, Nick Serra wrote: > A post save signal seems better suited for this. The post save signal > has an attribute 'created' that will be true or false depending on if > the object is being cr

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Nick Serra
A post save signal seems better suited for this. The post save signal has an attribute 'created' that will be true or false depending on if the object is being created or updated. Check out the post_save documentation: http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post

Re: Performing an action on model.save() but not on update

2010-04-23 Thread Skylar Saveland
On Apr 23, 3:27 pm, Jim N wrote: > Hi, > > I have overridden the default save() on a model so that I can update > some counts every time a save occurs.  Unfortunately, I don't want to > perform these actions every time the model is updated, which seems to > happen. > > Is there another approach