查询django中的模型(两级深)

时间:2013-03-21 16:37:27

标签: python django

如果我有以下表格

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()

如果我拥有城镇的名称/ ID,如何获得House中的所有Town

1 个答案:

答案 0 :(得分:15)

这应该可以解决问题:

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