如何访问数据ManyToOneForeignKey无鹡鸰做额外的查询(Django的) [英] How to access ManyToOneForeignKey data without making extra queries in Wagtail(django)
问题描述
我在我的app.models以下两个类和我使用的API鹡鸰,以获得数据作为JSON
类国家(页):
名称= models.CharField(MAX_LENGTH = 50)类城市(页):
高清get_state_name(个体经营):
回报self.state.name
名称= models.CharField(MAX_LENGTH = 50)
状态= models.ForeignKey(美国,related_name ='related_cities')
STATE_NAME =属性(get_state_name)
所以,当我尝试/api/v1/pages/?type=dashboard.States&fields=name,related_cities,它返回以下数据:
{
元:{
TOTAL_COUNT:1
},
页:
{
标识:1,
名:状态1
元:{
类型:dashboard.States,
detail_url:HTTP://本地主机:8000 / API / V1 /页/ 2601 /
},
related_cities:[
{
ID:28,
元:{
类型:dashboard.Cities,
detail_url:HTTP://本地主机:8000 / API / V1 /页/ 28 /
}
},
{
ID:37,
元:{
类型:dashboard.Cities,
detail_url:HTTP://本地主机:8000 / API / V1 /页/ 37 /
}
},
]
}
]
}
在related_cities领域,它返回 ID
和城市元
。我怎么能在这里得到响应的城市的名称,而不做额外的查询? :/
我找不到在的文档。我缺少的东西吗?
我想回应类似这样
related_cities:[
{
ID:28,
名:SomeCityName1
元:{
类型:dashboard.Cities,
detail_url:HTTP://本地主机:8000 / API / V1 /页/ 28 /
}
},
{
ID:37,
名:SomeCityName2
元:{
类型:dashboard.Cities,
detail_url:HTTP://本地主机:8000 / API / V1 /页/ 37 /
}
},
]
添加名字里是不可能的,而不必重写鹡鸰的API模块的位。
但是,这是我们正在努力的API(见第2版:<一href=\"https://github.com/kaedroho/weps/blob/api-fields/draft/005-wagtail-api-fields.rst#nested-objects\" rel=\"nofollow\">https://github.com/kaedroho/weps/blob/api-fields/draft/005-wagtail-api-fields.rst#nested-objects).
I have the following two classes in my app.models and i'm using the wagtail APIs to get the data as 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)
So, when I try /api/v1/pages/?type=dashboard.States&fields=name,related_cities, it returns the following data:
{
"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/"
}
},
]
}
]
}
In the related_cities field, it returns id
and meta
of the cities. How can I get the name of the city in the response here, without making an extra query? :/
I couldn't find any solution in the Documentation. Am I missing something? I want the response something like this
"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/"
}
},
]
Adding the name there isn't possible without having to override bits of Wagtail's API module.
But this is something we are working on for version 2 of the API (see: https://github.com/kaedroho/weps/blob/api-fields/draft/005-wagtail-api-fields.rst#nested-objects).
这篇关于如何访问数据ManyToOneForeignKey无鹡鸰做额外的查询(Django的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!