我的app.models中有以下两个类,我使用wagtail API将数据作为json
class States(Page):
name=models.CharField(max_length=50)
class Cities(Page):
def get_state_name(self):
return self.state.name
name = models.CharField(max_length=50)
state = models.ForeignKey(States, related_name='related_cities' )
state_name = property(get_state_name)
因此,当我尝试/api/v1/pages/?type=dashboard.States&fields=name,related_cities时,它会返回以下数据:
{
"meta": {
"total_count": 1
},
"pages": [
{
"id": 1,
"name": "State1",
"meta": {
"type": "dashboard.States",
"detail_url": "http://localhost:8000/api/v1/pages/2601/"
},
"related_cities": [
{
"id": 28,
"meta": {
"type": "dashboard.Cities",
"detail_url": "http://localhost:8000/api/v1/pages/28/"
}
},
{
"id": 37,
"meta": {
"type": "dashboard.Cities",
"detail_url": "http://localhost:8000/api/v1/pages/37/"
}
},
]
}
]
}
在related_cities字段中,它会返回城市的id
和meta
。如何在此处获取响应中的城市名称,而无需进行额外查询? :/
我在Documentation找不到任何解决方案。我错过了什么吗? 我想要这样的回复
"related_cities": [
{
"id": 28,
"name": "SomeCityName1",
"meta": {
"type": "dashboard.Cities",
"detail_url": "http://localhost:8000/api/v1/pages/28/"
}
},
{
"id": 37,
"name": "SomeCityName2",
"meta": {
"type": "dashboard.Cities",
"detail_url": "http://localhost:8000/api/v1/pages/37/"
}
},
]