:) On Apr 26, 9:48 am, Jim N <jim.nach...@gmail.com> 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 <nickse...@gmail.com> wrote: > > > > > > > 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(_hook_post_save_answer, sender=Answer) > > > On Apr 23, 6:20 pm, Jim N <jim.nach...@gmail.com> wrote: > > > > 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() > > > instance.question.increment_responses() > > > > post_save.connect(_hook_post_save_answer, sender=Answer) > > > >http://dpaste.de/897o/ > > > > But now, it increments TWICE! > > > > Is there some glaring error in my logic above? > > > > On Apr 23, 4:50 pm, Nick Serra <nickse...@gmail.com> wrote: > > > > > 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 <skylar.savel...@gmail.com> wrote: > > > > > > Yeah, and I think my suggestion fails for a user-defined rather than > > > > > auto-incrementing pk. > > > > > > On Apr 23, 4:21 pm, Nick Serra <nickse...@gmail.com> 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 created or updated. Check out the post_save > > > > > > documentation:http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.si... > > > > > > > On Apr 23, 3:32 pm, Skylar Saveland <skylar.savel...@gmail.com> > > > > > > wrote: > > > > > > > > On Apr 23, 3:27 pm, Jim N <jim.nach...@gmail.com> 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 I can take that distinguishes between > > > > > > > > save > > > > > > > > and update? > > > > > > > > > class Answer(models.Model): > > > > > > > > answer = models.TextField() > > > > > > > > user = models.ForeignKey(User, null=True) > > > > > > > > submit_date = models.DateTimeField('Date Submitted', > > > > > > > > default=datetime.datetime.now) > > > > > > > > question = models.ForeignKey(Question) > > > > > > > > permalink = models.CharField(max_length=300, blank=True) > > > > > > > > > def save(self, *args, **kwargs): > > > > > > > > super(Answer, self).save(*args, **kwargs) # Call the > > > > > > > > "real" > > > > > > > > save() method. > > > > > > > > if(self.user): > > > > > > > > quser = self.user.get_profile() > > > > > > > > quser.increment_answers() # <-- I don't want to > > > > > > > > do this > > > > > > > > on an update. > > > > > > > > self.question.increment_responses() # <-- I don't > > > > > > > > want to > > > > > > > > do this either. > > > > > > > > > Or in more readable form:http://dpaste.de/I2IR/ > > > > > > > > Before you call super you can see if bool(self.pk) is True or > > > > > > > False. > > > > > > > It will be False before the first save. > > > > > > > > -- > > > > > > > You received this message because you are subscribed to the > > > > > > > Google Groups "Django users" group. > > > > > > > To post to this group, send email to > > > > > > > django-us...@googlegroups.com. > > > > > > > To unsubscribe from this group, send email to > > > > > > > django-users+unsubscr...@googlegroups.com. > > > > > > > For more options, visit this group > > > > > > > athttp://groups.google.com/group/django-users?hl=en. > > > > > > > -- > > > > > > You received this message because you are subscribed to the Google > > > > > > Groups "Django users" group. > > > > > > To post to this group, send email to django-us...@googlegroups.com. > > > > > > To unsubscribe from this group, send email to > > > > > > django-users+unsubscr...@googlegroups.com. > > > > > > For more options, visit this group > > > > > > athttp://groups.google.com/group/django-users?hl=en. > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > > > Groups "Django users" group. > > > > > To post to this group, send email to django-us...@googlegroups.com. > > > > > To unsubscribe from this group, send email to > > > > > django-users+unsubscr...@googlegroups.com. > > > > > For more options, visit this group > > > > > athttp://groups.google.com/group/django-users?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > > > Groups "Django users" group. > > > > To post to this group, send email to django-us...@googlegroups.com. > > > > To unsubscribe from this group, send email to > > > > django-users+unsubscr...@googlegroups.com. > > > > For more options, visit this group > > > > athttp://groups.google.com/group/django-users?hl=en. > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Django users" group. > > > To post to this group, send email to django-us...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > django-users+unsubscr...@googlegroups.com. > > > For more options, visit this group > > > athttp://groups.google.com/group/django-users?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To post to this group, send email to django-us...@googlegroups.com. > > To unsubscribe from this group, send email to > > django-users+unsubscr...@googlegroups.com. > > For more options, visit this group > > athttp://groups.google.com/group/django-users?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group > athttp://groups.google.com/group/django-users?hl=en.
-- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.