On Mar 11, 2011, at 5:02 AM, pols wrote: > hai > I have two models as shown belove > > from django.db import models > from django.db.models import Sum > # Create your models here. > class polls(models.Model): > question = models.CharField(max_length=200) > pubish_date = models.DateTimeField('Date published') > > def __unicode__(self): > return self.question > def getCount(self): > count = > choice.objects.filter(polls=self.id).annotate(test=Sum('vote')) > return count.test > getCount.allow_tags = 'true' > getCount.short_description = 'Total Answers' >
> > class choice(models.Model): > polls = models.ForeignKey(polls) > choice = models.CharField(max_length=200) > vote = models.IntegerField() > > > > Here i need to get sum of votes for each question.So I need to edit > the code in the function getCount(). I try with the above code but > getting 'none'.then what may be the problem??.This is for the listing > in django admin polls listing > # if you have a poll (this could also be the 'self' in your method) poll = polls.objects.get(1) choice.objects.filter(polls = poll).aggregate(Sum('vote')) SELECT SUM("polls_choice"."vote") AS "vote__sum" FROM "polls_choice" WHERE "polls_choice"."polls_id" = 1 or poll.choice_set.aggregate(Sum('vote')) SELECT SUM("polls_choice"."vote") AS "vote__sum" FROM "polls_choice" WHERE "polls_choice"."polls_id" = 1 you would use annotate to add columns to a queryset polls.objects.annotate((Sum('choice__vote')) SELECT "polls_polls"."id", "polls_polls"."question", "polls_polls"."pubish_date", SUM("polls_choice"."vote") AS "choice__vote__sum" FROM "polls_polls" LEFT OUTER JOIN "polls_choice" ON ("polls_polls"."id" = "polls_choice"."polls_id") GROUP BY "polls_polls"."id", "polls_polls"."question", "polls_polls"."pubish_date", "polls_polls"."id", "polls_polls"."question", "polls_polls"."pubish_date" Jason > -- > 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. > -- 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.