Python 我们如何定义接受参数的@list\u路由
在我的应用程序中,我有一个Python 我们如何定义接受参数的@list\u路由,python,django,serialization,django-rest-framework,Python,Django,Serialization,Django Rest Framework,在我的应用程序中,我有一个ModelViewSet,其中有一个@list\u route()定义的函数,用于获取列表,但具有不同的序列化程序 class AnimalViewSet(viewsets.ModelViewSet): """ This viewset automatically provides `list`, `create`, `retrieve`, `update` and `destroy` actions. """ queryset
ModelViewSet
,其中有一个@list\u 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
},
另一个是名为listview
的@list\u route()
定义函数,它可能有一个端点/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\u route()
函数,它接受一个参数并使用AnimalListingSerializer
来过滤模型对象的查询集
结果。为像我们这样的初学者提供帮助的工作
@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"
}
},
但我知道我的语法是错误的,但这就是我要说的。
请帮忙
view函数中的参数是为URL引用保留的。也就是说,路由/5将被传递给一个以pk为参数的视图函数
def get(self, request, pk):
# get animal with pk
return animal with pk
您可以通过查询参数将参数传递到url
/anives/listview/?speceis_type=cow&breed=braham
然后使用请求对象在视图中访问它
request.query_-params['speceis\u-type']
和request.query_-params['braham']
或者您可以使用有文档记录的django rest过滤器中间件