I'm trying to figure out the best way to derive a nested menu from a set of non-nested models. Given a layout something like this:
class Beverage(models.Model):
country = models.ForeignKey(Country,null=True,blank=True)
region = models.ForeignKey(Region,null=True,blank=True)
subregion = models.ForeignKey(SubRegion,null=True,blank=True)
in_stock = models.BooleanField()
...
The resulting menu will be something like:
France
Region 1
Subregion 1
Subregion 2
Region 2
Subregion 3
Subregion 4
Spain
....
No country, region, or subregion should appear in the menu if there are no beverages in it that are not in stock. Because a subregion always belongs to a region and a region always belongs to a country, my initial approach was to nest the models themselves, and only put SubRegion on Beverage. Region and Country would then always be known by the subregion of the beverage. Unfortunately there are too many real-world exceptions to make this viable - wines with a region but not a subregion, etc. So I flattened the layout as above.
The question now is how to derive the menu from this model layout. It's looking like a list of deeply nested querysets is going to be the way to go but that seems computationally expensive and complex code-wise. Is there a cleaner way?