Given the following models, I need to return a list of Links for each Place, grouped by Category.
class Place(models.Model):
name = models.CharField(max_length=100)
class Category(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class Link(models.Model):
name = models.CharField(max_length=100)
url = models.URLField()
place = models.ManyToManyField( Place )
categories = models.ManyToManyField( Category )
description = models.TextField()
featured = models.BooleanField()
published = models.BooleanField()
date_added = models.DateField(auto_now_add=True)
The result would look something like:
Some Place
Information about Some Place ... (not really important to question)
Banks
Bank of America
Commerce Bank
First National Bank
Financial Services
Bank of America
Edward Jones
Loans
Commerce Bank
First National Bank
As you can see, a Link should be listed in every Category in which it belongs.
I tried to loop through categories and concatenate the querysets, but with ManyToMany fields, it doesn t seem possible to do the sort necessary that can be passed into the template and grouped there.