DRF-用于查询参数验证的基本Viewset [英] DRF - Base Viewset for query param validation

查看:323
本文介绍了DRF-用于查询参数验证的基本Viewset的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.9和Django Rest Framework 3.3

I am using Django 1.9 and Django Rest Framework 3.3

我正在创建三个相似但仍然足够不同的新端点,我希望它们成为自己的视图集以避免混乱的代码.它们没有模型支持,因此我直接扩展ViewSet.所有三个端点之间的相似点是它们需要相同的两个查询参数.所以我发现我正在这样做:

I'm creating three new endpoints that are similar but still different enough that I want them to be their own viewsets to avoid messy code. They are not backed by a model so I am extending ViewSet directly. One similarity between all three endpoints is they require the same two query parameters. So I find I am doing this:

class MyFirstViewset(viewsets.ViewSet):

    def list(self, request):
        some_param = request.query_params.get('someparam')
        if not some_param:
            return Response('someparam query parameter is required', status.HTTP_400_BAD_REQUEST)
        some_other_param = request.query_params.get('someotherparam')
        if not some_other_param:
              return Response('someotherparam query parameter is required', status.HTTP_400_BAD_REQUEST)


class MySecondViewset(viewsets.ViewSet):

    def list(self, request):
        some_param = request.query_params.get('someparam')
        if not some_param:
            return Response('someparam query parameter is required', status.HTTP_400_BAD_REQUEST)
        some_other_param = request.query_params.get('someotherparam')
        if not some_other_param:
              return Response('someotherparam query parameter is required', status.HTTP_400_BAD_REQUEST)

如您所见,它不是很干.一个明显的解决方案是使用基本视图集进行验证的相似部分的继承,问题是我不确定用DRF进行此类操作的最佳实践是什么.我要创建一个受保护的访问验证功能并调用它吗?但是,如果我所做的只是检查字典中的键,它甚至不必位于ViewSet中,对吗?只是让最后一个类在list()中进行验证,然后在所有子视图集中调用它,对我来说也很奇怪,因为那时我正在使用list()进行验证,而实际上没有返回基类中的任何内容.

As you can see... it's not very DRY. An obvious solution would be inheritance with a base viewset doing the similar parts of validation, the problem is I'm not sure what the best practice is for doing something like this with DRF. Would I want to make a protected access validation function and call that? But if all I was doing was checking a dict for keys it wouldn't even have to be in a ViewSet right? Just having the last class do the validation in list() then calling that in all the child viewsets also seems odd to me since then I am using list() to validate and not actually return anything in the base class.

有人可以告诉我对此有什么好方法吗?这种情况下的最佳做法是什么?我在Google周围搜索,但找不到任何东西.任何意见,将不胜感激.谢谢!

Could anyone tell me what a good approach to this would be? What's best practice for cases like this? I Googled around but couldn't find anything. Any advice would be appreciated. Thanks!

推荐答案

Class Base class之所以更好的一个原因是,您可以添加自己的派生或至少添加MixIns,因此从纯编码的角度来看,我认为您应该添加自己的MyBaseViewSet并从中派生MyFirstViewsetMySecondViewset.

One reason for why Class Base class are better is exactly that you can add your own derivation or at least add MixIns, so from the pure coding point of view, I think you should just add your own MyBaseViewSet and have MyFirstViewset and MySecondViewset derive from it.

也就是说,考虑覆盖get_queryset而不是list,并使用ListAPIView,这样您将获得一些默认行为,只是返回一个空数组,或者如果您确实要通过异常返回错误,请执行以下操作:

That said, consider overwriting get_queryset instead of list, and use ListAPIView so you get some default behavior, and just return an empty array or if you really want to return an error, through an exception:

class MyBaseViewSet(generics.ListAPIView):
    serializer_class = YourSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        some_param = self.request.query_params.get('someparam')
        if not some_param:
             raise exceptions.PermissionDenied

这篇关于DRF-用于查询参数验证的基本Viewset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆