我们如何定义接受参数的@list_route [英] How do we define @list_route that accept arguments
问题描述
在我的应用程序中,我有一个 ModelViewSet
与一个 @list_route()
定义的函数来获取列表,但是不同序列化器。
In my application i have this ModelViewSet
with one @list_route()
defined function for getting list but with different serializer.
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)
默认 AnimalViewSet
与此 / api / animal /
终点产生此序列化数据结果基于 AnimalSerializer
定义。
The Default AnimalViewSet
with this /api/animal/
end point yield this serialized data result as based on AnimalSerializer
definition.
{
"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
结构中定义的结果。
And the other one which is a @list_route()
defined function named listview
may have this end point /api/animal/listview/
which yields this result as defined in AnimalListingSerializer
structure.
{
"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
结果。对于像我们这样的初学者,我的帮助工作。
Now what i am trying to do is i want to define another @list_route()
function that takes an argument and uses AnimalListingSerializer
in order to filter the query_set
result of the model object. A work around my help for a beginner like us.
@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 code>和
args2 =婆罗门
。我希望这个结果。
Let us assumed that args1 = "Cow"
and args2 = "Brahman"
. And i am expecting this result.
{
"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.
推荐答案
视图功能中的参数保留用于URL引用。即路线动物/ 5将传递给具有pk作为参数的视图函数。
parameters in the view function are reserved for URL references. ie the route animals/5 would be passed to a view function that has pk as an argument.
def get(self, request, pk):
# get animal with pk
return animal with pk
通过使用@list_route装饰,您可以选择通过您的URL提供任何其他参数。在您的情况下,您可以通过URL请求的唯一资源是listview;所以 / animals / listview
。你有其他的选择,但如果你想继续下去这个路径。您可以通过url将参数传递给您的url,查询参数如 / animals / listview /?speceis_type = cow& breed = braham
,然后通过 request.query_params ['speceis_type']
和 request.query_params ['braham']
或者您可以使用django rest过滤器中间件记录在案 here
by decorating with an @list_route you over ride the option to provide any additional params via your URL. In your case the only resource you can request via the url is listview; so /animals/listview
. You have other options though if you want to continue down this path. You can pass parameters to your url via a url, query param like /animals/listview/?speceis_type=cow&breed=braham
then access it in your view via request.query_params['speceis_type']
and request.query_params['braham']
or you can use the django rest filter middleware that is documented here
这篇关于我们如何定义接受参数的@list_route的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!