Querying models in django (two levels deep)

django, python

Solution

This should do the trick:

town_id = 5
houses_in_town = House.objects.filter(street__town__id = town_id)

Problem

If I have the following tables ``` class Town(models.Model): created = models.DateTimeField() class Street(models.Model): town = models.ForeignKey(Town) created = models.DateTimeField() class House(models.Model): street = models.ForeignKey(Street) created = models.DateTimeField() ``` How do I get all the `House`s in a `Town` if I have the name/id of the town?

Original source