hi,

yes, it is called in models.py, and no, it's not indented and neither
is the handler function.

On Sep 14, 4:28 pm, Jason <goodri...@gmail.com> wrote:
> Is the m2m_changed() call in the models.py?
>
> Also, it looks indented - if it is, it shouldn't be - in other words -
> it doesn't belong to the model.
>
> On Sep 13, 1:27 pm, allyb <beanb...@googlemail.com> wrote:
>
> > I'm finding it difficult to denormalise a field in a django model. I
> > have:
>
> >     class AnswerSet(models.Model):
> >         title = models.CharField(max_length=255)
> >         num_answers = models.PositiveIntegerField(editable=False,
> > default=0)
> >         answers = models.ManyToManyField(Answer,
> > through='AnswerSetAnswer')
> >         ...
>
> >     class AnswerSetAnswer(models.Model):
> >         answer = models.ForeignKey(Answer)
> >         answer_set = models.ForeignKey(AnswerSet)
> >         ...
>
> > I want num_answers to contain a count of the number of answers in the
> > set.
>
> > If 5 answers are initially associated with the AnswerSet "Food" and I
> > edit one so it becomes associated with the AnswerSet "Colours", how
> > can I recalculate the number of answers in the AnswerSet with "Food"?
> > All the signals only seem to send the new data so I can't just
> > override the save method.
>
> > I've tried using the m2m_changed signal, but it never gets called when
> > I edit relationships through the admin form.
>
> > Here's my code anyway:
>
> >     def update_answer_set_num_answers(sender, **kwargs):
> >         """
> >         Updates the num_answers field to reflect the number of answers
> >         associated with this AnswerSet
> >         """
> >         print "hello"                                              #
> > never gets here
> >         instance = kwargs.get('instance', False)
>
> >         print "no instance"
>
> >         if not instance:
> >             return
>
> >         action = kwargs.get('action')
>
> >         print "action: ", action
>
> >         if (action != 'pre_remove' and action != 'pre_add' and action !
> > = 'clear'):
> >             return
>
> >         reverse = kwargs.get('reverse')
>
> >         if reverse:
> >             answer_set = instance.answer_set
> >         else:
> >             answer_set = instance.answer_set
> >         num_answers =
> > AnswerSetAnswer.objects.filter(answer_set=answer_set.id).count()
>
> >         if (action == 'pre_remove'):
> >             num_answers -= int(kwargs.get('pk_set'))
> >         elif (action == 'pre_add'):
> >             num_answers += int(kwargs.get('pk_set'))
> >         elif (action == 'clear'):
> >             num_answers = 0
>
> >         answer_set.num_answers = num_answers
>
> >         print 'n a: ', answer_set.num_answers
> >         answer_set.save()
>
> >     m2m_changed.connect(update_answer_set_num_answers, \
> >       AnswerSet.answers.through)
>
> > Setting weak=False or weak=True makes no difference.
>
> > How can I get the signal to work?
>
> > Thanks

-- 
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.

Reply via email to