Let's say I have the following models: class Group(models.Model): group_name = models.CharField(max_length=20)
class Person(models.Model): group = models.ForeignKey(Group) name = models.CharField(max_length=50) How do I get an output of the following? GroupA: John, Stacy, Pete GroupB: Bob, Bill, Mary I want to cycle through the groups and then output the people in each group. In rough pseudo-code: groups = Group.objects.all() for g in groups: print g.group_name, ": " for p in g.person_set.all: print p.person However, this leads to a query being run for each person in order to access the person's name, which I find to be really unnecessary. I only want to run one query per group -- will I have to use raw SQL to do this? My initial thought was to run a query that returns all the people in a group, and then cycling through the queryset to append the names into a list and then attaching the list to the project object. -- 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.