如何防止主键附加到Django Rest Framework URL

时间:2019-07-03 20:11:57

标签: django django-rest-framework

我有一个django项目,我正在尝试将django rest框架集成到其中。我目前有一个视图集,我想做两件事。我想将两个过滤器参数添加到url中,并在视图集中获取它们,并在queryset中使用它们来过滤与那些过滤器匹配的记录。我还想防止在尝试访问视图集时将主键过滤器附加到url ...

以下是网址:

router.register(r'preferences/(?P<namespace>.+)/(?P<path>.+)', PreferencePathViewSet, basename='Preference-path')
router.register(r'preferences/(?P<namespace>.+)', PreferenceNamespaceViewSet, basename='Preference-namespace')
router.register(r'preferences', PreferenceUserViewSet, basename='Preference')

以下是视图集:

class PreferenceUserViewSet(viewsets.ModelViewSet):

    model = Preference
    serializer_class = PreferenceSerializer

    def get_permissions(self):
        if self.action == 'create' or self.action == 'destroy':
            permission_classes = [IsAuthenticated]
        else: 
            permission_classes = [IsAdminUser]
        return [permission() for permission in permission_classes]

    @permission_classes((IsAuthenticated))
    def get_queryset(self):
        queryset = Preference.objects.filter(user_id=1)
        return queryset

class PreferenceNamespaceViewSet(viewsets.ModelViewSet):

    model = Preference
    serializer_class = PreferenceSerializer
    lookup_namespace = 'namespace'

    def get_permissions(self):
        if self.action == 'create' or self.action == 'destroy':
            permission_classes = [IsAuthenticated]
        else: 
            permission_classes = [IsAdminUser]
        return [permission() for permission in permission_classes]

    @permission_classes((IsAuthenticated))
    def get_queryset(self):
        namespace = self.request.query_params.get('namespace')
        queryset = Preference.objects.filter(user_id=1, namespace=namespace)
        return queryset

class PreferencePathViewSet(viewsets.ModelViewSet):

    model = Preference
    serializer_class = PreferenceSerializer
    lookup_namespace = 'namespace'
    lookup_path = 'path'

    def get_permissions(self):
        if self.action == 'create' or self.action == 'destroy':
            permission_classes = [IsAuthenticated]
        else: 
            permission_classes = [IsAdminUser]
        return [permission() for permission in permission_classes]

    @permission_classes((IsAuthenticated))
    def get_queryset(self):
        namespace = self.request.query_params.get('namespace')
        path = self.request.query_params.get('path')
        queryset = Preference.objects.filter(user_id=1, namespace=namespace, path=path)
        return queryset

这是在django rest框架后端可访问的所有URL的列表:

api/v2/ ^preferences/(?P<namespace>.+)/(?P<path>.+)/$ [name='Preference-path-list']
api/v2/ ^preferences/(?P<namespace>.+)/(?P<path>.+)/(?P<pk>[^/.]+)/$ [name='Preference-path-detail']
api/v2/ ^preferences/(?P<namespace>.+)/$ [name='Preference-namespace-list']
api/v2/ ^preferences/(?P<namespace>.+)/(?P<pk>[^/.]+)/$ [name='Preference-namespace-detail']
api/v2/ ^preferences/$ [name='Preference-list']
api/v2/ ^preferences/(?P<pk>[^/.]+)/$ [name='Preference-detail']

如果有一种方法可以使单个视图集根据传递到url中的两个参数显示过滤后的结果,则将使生活更加轻松。

现在,如果我只做preferences/,我会得到过滤后的结果。 如果我做preferences/namespace1,即使有将命名空间设置为namespace1的结果,我也没有任何结果。如果我做preferences/namespace1/path1

,也会发生同样的事情

我希望这些参数用作过滤器。我不确定是否将主键附加到导致问题的url上...

0 个答案:

没有答案