【问题标题】:How do we define @list_route that accept arguments我们如何定义接受参数的@list_route
【发布时间】:2016-11-17 15:37:39
【问题描述】:

在我的应用程序中,我有这个 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"
    }
},

但我知道我的语法是错误的,但这就是我要说的。 请帮忙。

【问题讨论】:

    标签: python django serialization django-rest-framework


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-01
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 2020-12-16
      • 2013-10-01
      相关资源
      最近更新 更多