我有三个相关的模型,比如
class City(models.Model):
name = models.CharField(max_length=200, blank=False)
country = models.ForeignKey(Country,unique=False,null=False)
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class County(models.Model):
name = models.CharField(max_length=500, blank=False)
city = models.ForeignKey(City,unique=False,null=False)
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
class District(models.Model):
name = models.CharField(max_length=500, blank=False)
county = models.ForeignKey(County,unique=False,null=False)
def __unicode__(self):
return self.name
class Meta:
ordering = ['name']
我想做的是获得Districts
指定城市的所有信息。我试过 :
District.objects.all().filter(county = County.objects.all().filter(city=City.objects.filter(id=4)))
但是,它会给出错误,例如OperationalError: (1242, 'Subquery returns more than 1 row')
你能告诉我如何在 django 中完成这个查询吗?
谢谢