如何使用drf-multiple-model模块修复URL数据检索?

时间:2019-06-25 10:19:26

标签: python django python-3.x django-rest-framework multiple-models

我正在尝试使用drf-multiple-model模块创建一个API,但是它正在使用单个实例URL产生问题,例如http://localhost:8000/categories/partner-spec-categories/ /中没有返回可用数据。问题可能在哪里?

我正在运行Python 3.6.5,Django 2.2.2,MySQL 8.0.16。

截至目前,我只关注ott.cat.master.viewset,这就是为什么其他路由器没有base_name的原因。

urls.py

router = routers.DefaultRouter()
router.register(r'general-categories', gen.cat_master_viewset.CatMasterView)
router.register(r'gen-cat-settings', gen.cat_settings_viewset.CatSettingsView)
router.register(r'partner-spec-catagories', ott.cat_master_viewset.CatMasterView,base_name='partner-spec-catagories')
router.register(r'spec-cat-settings', ott.cat_settings_viewset.CatSettingsView)

我遇到的viewset.py文件

class CatMasterView(FlatMultipleModelAPIViewSet,viewsets.ModelViewSet):

    all_or_none_response = False

    def partner_id_filter(queryset,request,*args,**kwargs):
        global all_or_none_response
        final_queryset = queryset
        if PartnerMaster.objects.filter(partner_id=request.query_params.get('partner_id')).exists():
            partner_to_get = request.query_params.get('partner_id')
            if partner_to_get == None:
                all_or_none_response = False
            else:
                all_or_none_response = True
                final_queryset = final_queryset.filter(partner_id=partner_to_get)
        if OttCategoryMaster.objects.filter(id=request.query_params.get('id')).exists():
            id_to_get = request.query_params.get('id')
            if id_to_get == None:
                all_or_none_response = False
            else:
                all_or_none_response = True
                final_queryset = final_queryset.filter(id=id_to_get)
        if OttCategoryMaster.objects.filter(name=request.query_params.get('name')).exists():
            name_to_get = request.query_params.get('name')
            if name_to_get == None:
                all_or_none_response = False
            else:
                all_or_none_response = True
                final_queryset = final_queryset.filter(name=name_to_get)
        return final_queryset


    def all_or_none_response_filter(queryset,request,response=all_or_none_response,*args,**kwargs):

        if response:
            pass
        else:
            queryset = None

        if PartnerMaster.objects.filter(partner_id=request.query_params.get('partner_id')).exists():
            pass
        else:
            queryset = None

        return queryset

    querylist = [
                {'queryset':OttCategoryMaster.objects.all(),
                'serializer_class':ott_cat_master_serializer,
                'label':'ott_categories',
                'filter_fn':partner_id_filter,},

                {'queryset':CategoryMaster.objects.all(),
                'serializer_class':gen_cat_master_serializer,
                'label':'gen_categories',
                'filter_fn':all_or_none_response_filter}
                ]
    def get_serializer_class(self):
        return ott_cat_master_serializer

尽管过滤... /?id = 1可以,但使用... / 1 /却不起作用,

0 个答案:

没有答案