My model structure is like this: class Group(models.Model): u_id = models.ForeignKey(User) group_name = models.CharField(max_length=50,unique=True) def __str__(self): # __unicode__ on Python 2 return self.group_name + " " + self.org_nam class Members(models.Model): group_id = models.ForeignKey(Group) name = models.CharField(max_length=60) def __str__(self): # __unicode__ on Python 2 return self.name + " " + self.gender class Ratings(models.Model): member_id = models.ForeignKey(Members) question = models.ForeignKey(Questions) rating = models.DecimalField(max_digits=20,decimal_places=2,null=True,blank=True) class GroupQn(models.Model): group_id = models.ForeignKey(Group) qn_id = models.ForeignKey(Questions)
*I want calculate maximum rating member for each question of a particular group:* *What I have tried:* def top_ratings(group_id): group = get_object_or_404(Group, pk=group_id) members = Members.objects.filter(group_id=group) questionset = GroupQn.objects.filter(group_id=group) top_rated_list = [] for i in questionset: max_rating = Ratings.objects.filter(question=i.qn_id,member_id=members).aggregate(Max('rating'))['rating__max'] top_rated_list = Ratings.objects.get(rating = max_rating) return top_rated_list *i am getting an error:* *"*MultipleObjectsReturned at /group/2 get() returned more than one Ratings -- it returned 2! Request Method: GET Request URL: 127.0.0.1:8000/group/2 Django Version: 1.7.5 Exception Type: MultipleObjectsReturned Exception Value: get() returned more than one Ratings -- it returned 2! – *"* -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c8da7f1c-4f43-4d06-8ca2-a6b37c2a0776%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.