在Django REST框架的ModelViewSets中可以使用继承的概念吗?

huangapple go评论70阅读模式
英文:

Can we use the concept of inheritance in Django rest framework in ModelViewSets?

问题

我正在一遍又一遍地重复我的代码,在不同的ModelViewSets中,唯一变化的是模型的名称。所以,我想为此编写一个基类,并将每个模型的名称作为参数传递,但它不起作用。

有任何帮助吗?

class RemitterQueryViewViewset(viewsets.ModelViewSet):
    queryset = RemitterQueryView.objects.all()
    serializer_class = RemitterQueryViewSerializer
    http_method_names = ['get']
    
    def get_base_path(self):
        splitted_url = self.request.build_absolute_uri().split('?')
        base_url = splitted_url[0]
        return base_url

    def get_queryset(self):
        limit = self.request.query_params.get('limit', 26)
        offset = self.request.query_params.get('offset', 0)
        filter_list = self.request.query_params.get('filter_list', None)
        sort_list = self.request.query_params.get('sort_list', None)
        if filter_list is None and sort_list is None:
            return RemitterQueryView.objects.filter(id__gt=int(offset))[0:int(limit)]

        elif filter_list is not None and sort_list is not None:
            filter_columns = self.get_filter_list()
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is None and sort_list is not None:
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset)).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is not None and sort_list is None:
            filter_columns = self.get_filter_list()
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)]
英文:

I'm repeating my code, again and again, in different ModelViewSets and the only thing that is changing here is the Model's name. So, I want to write a base class for this and send each model's name as an argument, but its not working.

Any help?

class RemitterQueryViewViewset(viewsets.ModelViewSet):
    queryset = RemitterQueryView.objects.all()
    serializer_class = RemitterQueryViewSerializer
    http_method_names = ['get']
    
    def get_base_path(self):
        splitted_url = self.request.build_absolute_uri().split('?')
        base_url = splitted_url[0]
        return base_url

    def get_queryset(self):
        limit = self.request.query_params.get('limit', 26)
        offset = self.request.query_params.get('offset', 0)
        filter_list = self.request.query_params.get('filter_list', None)
        sort_list = self.request.query_params.get('sort_list', None)
        if filter_list is None and sort_list is None:
            return RemitterQueryView.objects.filter(id__gt=int(offset))[0:int(limit)]

        elif filter_list is not None and sort_list is not None:
            filter_columns = self.get_filter_list()
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is None and sort_list is not None:
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset)).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is not None and sort_list is None:
            filter_columns = self.get_filter_list()
            # print(filter_columns)
            # print(MockData.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)])
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)]

答案1

得分: 1

你可以创建一个通用的混合类,并在所有的类视图中继承这个混合类。

class MixinView:

    def get_base_path(self):
        splitted_url = self.request.build_absolute_uri().split('?')
        base_url = splitted_url[0]
        return base_url

    def get_queryset(self):
        limit = self.request.query_params.get('limit', 26)
        offset = self.request.query_params.get('offset', 0)
        filter_list = self.request.query_params.get('filter_list', None)
        sort_list = self.request.query_params.get('sort_list', None)
        if filter_list is None and sort_list is None:
            return RemitterQueryView.objects.filter(id__gt=int(offset))[0:int(limit)]

        elif filter_list is not None and sort_list is not None:
            filter_columns = self.get_filter_list()
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is None and sort_list is not None:
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset)).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is not None and sort_list is None:
            filter_columns = self.get_filter_list()
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)]


class RemitterQueryViewViewset(MixinView, viewsets.ModelViewSet):
    queryset = RemitterQueryView.objects.all()
    serializer_class = RemitterQueryViewSerializer
    http_method_names = ['get']

在这里,你的 MixinView 中的 get_queryset 方法将会被执行。

英文:

You can create a common mixin class and inherit this mixin in all CBV.

class MixinView:

    def get_base_path(self):
        splitted_url = self.request.build_absolute_uri().split('?')
        base_url = splitted_url[0]
        return base_url

    def get_queryset(self):
        limit = self.request.query_params.get('limit', 26)
        offset = self.request.query_params.get('offset', 0)
        filter_list = self.request.query_params.get('filter_list', None)
        sort_list = self.request.query_params.get('sort_list', None)
        if filter_list is None and sort_list is None:
            return RemitterQueryView.objects.filter(id__gt=int(offset))[0:int(limit)]

        elif filter_list is not None and sort_list is not None:
            filter_columns = self.get_filter_list()
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is None and sort_list is not None:
            sort_columns = self.get_sort_list()
            sort_columns = list(itertools.chain(*sort_columns))
            return RemitterQueryView.objects.filter(id__gt=int(offset)).order_by(*sort_columns)[0:int(limit)]

        elif filter_list is not None and sort_list is None:
            filter_columns = self.get_filter_list()
            # print(filter_columns)
            # print(MockData.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)])
            return RemitterQueryView.objects.filter(id__gt=int(offset), **filter_columns)[0:int(limit)]


class RemitterQueryViewViewset(MixinView, viewsets.ModelViewSet):
    queryset = RemitterQueryView.objects.all()
    serializer_class = RemitterQueryViewSerializer
    http_method_names = ['get']

Here your get_queryset method from MixinView will get executed.

huangapple
  • 本文由 发表于 2020年1月3日 16:55:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定