这有点令人困惑,我有这些伪模型:
class Country(models.Model):
name = models.CharField(unique=True)
class Region(models.Model):
name = models.CharField(unique=True)
country = models.ForeignKey(Country)
class SubRegion(models.Model):
name = models.CharField(unique=True)
region = models.ForeignKey(Region)
class Estate(models.Model):
name = models.CharField(unique=True)
sub_region = models.ForeignKey(SubRegion)
我正在尝试 JSON 序列化他们的数据,如下所示。但是我不确定如何有效地做到这一点(避免过多的数据库查询),建议表示赞赏
{
CountryX: {
RegionX {
SubRegionX = [
"EstateX"
"EstateY",
"EstateZ"
],
SubRegionY = [ etc... ]
},
RegionY { etc... }
},
CountryY: { etc... }
}