基于URL的Django REST框架过滤器查询集

时间:2017-07-05 00:26:08

标签: python django django-rest-framework

当用户访问" baseurl / companies / 6 / inquiry /"时,我知道company_id是6。

然后,用户可以选择使用特定产品创建查询,但只能看到属于公司6的产品。

这是我的观点:

class InquirySerializer(serializers.ModelSerializer):
    def __init__(self, *args, company_id=None, **kwargs):
        super(InquirySerializer, self).__init__(*args, **kwargs)
        company_set = Company.objects.filter(pk=company_id)
        self.fields['company'].queryset = company_set

    company = serializers.HyperlinkedRelatedField(many=False,
                                                  view_name='company-detail',
                                                  queryset=Company.objects.all())
    inquirer = UserPKField(many=False)
    is_anonymous = serializers.BooleanField
    product_rows = CompanyProductField(many=True, company_id= 'Want to access company_id in __init__')

    class Meta:
        model = Inquiry
        fields = ('pk', 'company', 'inquirer_email', 'inquirer', 'is_anonymous', 'inquiry_date', 'product_rows')
        read_only_fields = ('inquirer', 'inquiry_date')

这是我的序列化程序:

class CompanyProductField(serializers.PrimaryKeyRelatedField):
    def __init__(self, *args, company_id=None, **kwargs):
        super(CompanyProductField, self).__init__(*args, **kwargs)
        self.company_id = company_id

    def get_queryset(self):
        product_query = Q(company__pk=self.company_id)
        return Product.objects.filter(product_query)

这里是CompanyProductField

{{1}}

必须有一种简单的方法可以访问已经在InquirySerializer的 init 方法中的company_id,然后将其传递给我,但我很难过。

3 个答案:

答案 0 :(得分:2)

class InquirySerializer(serializers.ModelSerializer):
    def __init__(self, *args, **kwargs):
        company_id = kwargs.pop('company_id')
        self.company_id = company_id
        super().__init__(*args, **kwargs)

    product_rows = CompanyProductField(many=True)

class CompanyProductField(serializers.PrimaryKeyRelatedField):
    def get_queryset(self):
        return Product.objects.filter(company_id=self.root.company_id)

在这种情况下,CompanyProductField类的self.root属性将引用InquirySerializer的实例。

答案 1 :(得分:1)

现在,我将使用这种有点“hacky”的方式来做这件事。

在我的serializers.py文件中,我添加了一个全局变量:

from rest_framework import serializers
from .models import *
from django.db.models import Q

global_company_id = 0

然后在我的序列化程序的init方法中,我设置了global_company_id:

class InquirySerializer(serializers.ModelSerializer):
def __init__(self, *args, company_id=None, **kwargs):
    super(InquirySerializer, self).__init__(*args, **kwargs)
    company_set = Company.objects.filter(pk=company_id)
    self.fields['company'].queryset = company_set
    global global_company_id
    global_company_id = company_id

company = serializers.HyperlinkedRelatedField(many=False,
                                              view_name='company-detail',
                                              queryset=Company.objects.all())
inquirer = UserPKField(many=False)
is_anonymous = serializers.BooleanField
product_rows = CompanyProductField(many=True)

在CompanyProductField中,我访问了global_company_id:

class CompanyProductField(serializers.PrimaryKeyRelatedField):
def get_queryset(self):
    product_query = Q(company__pk=global_company_id)
    return Product.objects.filter(product_query)

答案 2 :(得分:0)

您可以从self get_serializer()`方法中删除self.kwargs['company_id'] in your

def get_serializer(self, *args, **kwargs):
    serializer_class = self.get_serializer_class()
    kwargs['context'] = self.get_serializer_context()
    return serializer_class(company_id=kwargs['company_id'], *args, **kwargs)