On Thu, Jul 19, 2012 at 5:00 PM, karan <htc.karanagar...@gmail.com> wrote: > Hi, > > my models is- > > class Students(models.Model): > country = models.ManyToManyField( Country ) > > I want to calculate the total number of student objects for each of the > country objects. > > I need the count of students interested in each country so far in my > database. > > I was trying something with annotate but couldnt get far. > > I need suggestions :) > > Thanks . cheers ! >
Country.objects.annotate(num_students=Count('students')) https://docs.djangoproject.com/en/1.4/topics/db/aggregation/#cheat-sheet FYI: Your class name should be singular, as anything that 'Students' has a foreign key to would access the related student instances by 'foo_object.studentss_set' (note the double s). Cheers Tom -- 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.