如果我有以下表格
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
?
答案 0 :(得分:15)
这应该可以解决问题:
town_id = 5
houses_in_town = House.objects.filter(street__town__id = town_id)