gpt4 book ai didi

python - 我们如何定义接受参数的@list_route

转载 作者:行者123 更新时间:2023-11-28 17:22:39 25 4
gpt4 key购买 nike

在我的应用程序中,我有这个 ModelViewSet 和一个 @list_route() 定义的函数来获取列表,但有不同的序列化程序。

class AnimalViewSet(viewsets.ModelViewSet):
"""
This viewset automatically provides `list`, `create`, `retrieve`,
`update` and `destroy` actions.
"""
queryset = Animal.objects.all()
serializer_class = AnimalSerializer // Default modelviewset serializer

lookup_field = 'this_id'

@list_route()
def listview(self, request):
query_set = Animal.objects.all()
serializer = AnimalListingSerializer(query_set, many=True) // Serializer with different field included.
return Response(serializer.data)

具有此 /api/animal/ 端点的默认 AnimalViewSet 会根据 AnimalSerializer 定义产生此序列化数据结果。

{
"this_id": "1001",
"name": "Animal Testing 1",
"species_type": "Cow",
"breed": "Brahman",
...
"herd": 1
},
{
"this_id": "1004",
"name": "Animal Testing 2",
"species_type": "Cow",
"breed": "Holstien",
....
"herd": 1
},
{
"this_id": "1020",
"name": "Animal Testing 20",
"species_type": "Cow",
"breed": "Brahman",
....
"herd": 4
},

另一个是 @list_route() 定义的名为 listview 的函数可能有这个终点 /api/animal/listview/ 这会产生在 AnimalListingSerializer 结构中定义的结果。

{
"this_id": "1001",
"name": "Animal Testing 1",
"species_type": "Cow",
"breed": "Brahman",
....
"herd": {
"id": 1,
"name": "High Production",
"description": null
}
},
{
"this_id": "1004",
"name": "Animal Testing 2",
"species_type": "Cow",
"breed": "Holstien",
....
"herd": {
"id": 1,
"name": "High Production",
"description": null
}
},
{
"this_id": "1020",
"name": "Animal Testing 20",
"species_type": "Cow",
"breed": "Brahman",
....
"herd": {
"id": 4,
"name": "Bad Production",
"description": "Bad Production"
}
}

现在我想做的是定义另一个 @list_route() 函数,它接受一个参数并使用 AnimalListingSerializer 来过滤query_set 模型对象的结果。解决我对像我们这样的初学者的帮助

@list_route()
def customList(self, request, args1, args2):
query_set = Animal.objects.filter(species_type=args1, breed=args2)
serializer = AnimalListingSerializer(query_set, many=True)
return Response(serializer.data)

让我们假设 args1 = "Cow"args2 = "Brahman"。我期待着这个结果。

{
"this_id": "1001",
"name": "Animal Testing 1",
"species_type": "Cow",
"breed": "Brahman",
....
"herd": {
"id": 1,
"name": "High Production",
"description": null
}
},
{
"this_id": "1020",
"name": "Animal Testing 20",
"species_type": "Cow",
"breed": "Brahman",
....
"herd": {
"id": 4,
"name": "Bad Production",
"description": "Bad Production"
}
},

But i know my syntax is wrong, but that is what i am talking about. Please help.

最佳答案

View 函数中的参数保留用于 URL 引用。即路线 animals/5 将被传递给以 pk 作为参数的 View 函数。

def get(self, request, pk):
# get animal with pk
return animal with pk

您可以通过查询参数将参数传递给您的 url

/animals/listview/?speceis_type=cow&breed=braham

然后使用请求对象在您的 View 中访问它request.query_params['speceis_type']request.query_params['braham'] 或者您可以使用记录在案的 django rest 过滤器中间件 here

关于python - 我们如何定义接受参数的@list_route,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40659017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com