创建一个 Viewset Mixin/Baseclass 来继承 viewset-action

我有一个网站,本质上是我正在参与的 DnD 活动的 wiki。因此,它包含有关生物、角色、位置等的文章。我想使用 Viewset 轻松访问它们,并希望使用 Viewset 操作(与自定义路由器一起)能够查找单个记录,而不是通过 pk,而是通过各种查询参数。


我已经有一些适用于此的东西,现在我想对其应用一些继承,以免重复自己。我想做的是这样的:


class WikiBaseViewset (viewsets.ModelViewSet):

    detail_with_params_url_pattern_suffix: str


    @action(detail=True, url_name="detail-params", url_path=detail_with_params_url_pattern_suffix)

    def detail_through_params(self, request, **kwargs):

        if self.detail_with_params_url_pattern_suffix == "":

            raise InvalidViewsetURLException("URL of view 'detail_through_params' of WikiBaseViewset is not defined!")


        model = self.serializer_class.Meta.model

        instance = get_object_or_404(model, **kwargs)

        serializer = self.get_serializer(instance)

        return Response(serializer.data)



class CharacterSerializer (serializers.HyperlinkedModelSerializer):

    class Meta:

        model = wiki_models.Character

        fields = '__all__'



class CharacterViewSet(WikiBaseViewset):

    """Called with URLs: character, character/<str: name>"""

    serializer_class = CharacterSerializer

    queryset = wiki_models.Character.objects.all()

    detail_with_params_url_pattern_suffix = "(?P<name__iexact>.+)"


然而,我正在努力解决装饰器绝对需要基类中的 URL 参数这一事实。否则,由于 NameError 抱怨未定义,代码将无法编译detail_with_params_url_pattern_suffix。如果您要在基类中进行设置detail_with_params_url_pattern_suffix="",以便在编译代码时不出现错误,那仍然没关系,因为到目前为止我的实验中的装饰器仍然从 not 获取该变量的WikiBaseViewset值CharacterViewSet。


我如何重写我的 BaseClass 才能使其正常工作?还有办法吗?


互换的青春
浏览 111回答 1
1回答

慕少森

对于这个问题我没有找到完全令人满意的答案,但最终默许了这个解决方案,因为它比复制粘贴更好。您可能无法继承视图集操作,但您肯定可以继承各个方法,然后在子级中覆盖它们并在顶部抛出一个装饰器。这导致了这个结构:class WikiBaseViewset (viewsets.ModelViewSet):&nbsp; &nbsp; detail_with_params_url_pattern_suffix: str&nbsp; &nbsp; def detail_through_params(self, request, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; model = self.serializer_class.Meta.model&nbsp; &nbsp; &nbsp; &nbsp; instance = get_object_or_404(model, **kwargs)&nbsp; &nbsp; &nbsp; &nbsp; serializer = self.get_serializer(instance)&nbsp; &nbsp; &nbsp; &nbsp; return Response(serializer.data)class CharacterSerializer (serializers.HyperlinkedModelSerializer):&nbsp; &nbsp; class Meta:&nbsp; &nbsp; &nbsp; &nbsp; model = wiki_models.Character&nbsp; &nbsp; &nbsp; &nbsp; fields = '__all__'class CharacterViewSet(WikiBaseViewset):&nbsp; &nbsp; """Called with URLs: character, character/<str: name>"""&nbsp; &nbsp; serializer_class = CharacterSerializer&nbsp; &nbsp; queryset = wiki_models.Character.objects.all()&nbsp; &nbsp; @action(detail=True, url_name="detail-params", url_path="(?P<name__iexact>.+)")&nbsp; &nbsp; def detail_through_params(self, request, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; return super().detail_through_params(request, **kwargs)
打开App,查看更多内容
随时随地看视频慕课网APP