'Django - Counting filtered model instances per row in a template table

Let's say I have the following django models:

class House(Model):
    author = ForeignKey(settings.AUTH_USER_MODEL, on_delete=CASCADE)
    title = CharField(max_length=200, default='')
    text = TextField(default='')
    rooms = IntegerField(null=True)

class Room(Model): 
    author = ForeignKey(settings.AUTH_USER_MODEL, on_delete=CASCADE)
    title = CharField(max_length=200, default='')
    text = TextField(default='')
    house = CharField(max_length=200, default='')
    furniture = IntegerField(null=True)

class Furniture(model): 
    author = ForeignKey(settings.AUTH_USER_MODEL, on_delete=CASCADE)
    title = CharField(max_length=200, default='')
    text = TextField(default='')
    room = CharField(max_length=200, default='')

And I want to generate the following table in a template:

Room In house Amount of furniture
Living Room Summer house 5
Kitchen Summer house 8
Bedroom Summer house 2
Bathroom Main house 3

Where the column "Amount of furniture" counts the instances of the "furniture" model where the field "room" is equal to that of the entry in the column "room" for that row.

I'm trying to figure out how to do this, and I've landed on a few different ways - ranked from most ideal/pythonic to least ideal/pythonic.

  1. Building some sort of mechanism into the model. This would be perfect, but I can't seem to find any obvious way to do it.
  2. Adding a function that generates a dictionary in the view in views.py. Would be easy to build (gather names of "room", make a for loop doing a filter query for each room on the model "furniture", add a counter variable, and build a dictionary) but not very flexible or pythonic.
  3. Using a third party module like datatables. Feels like overkill - but may be a better long term solution?
  4. Setting up some real shenaningans in the template language. Since you can't declare variables in the template, i imagine this would be a spider web of nested loops, conditionals, and custom template tags.

Which approach should I go for here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source