如何使用模型字段过滤serializers.SlugRelatedField queryset

时间:2015-04-15 05:42:58

标签: django serialization django-rest-framework

我无法使用queryset参数过滤SlugRelatedField的可能选项。这是我的序列化器

class AttendeeProfileSerializer(serializers.HyperlinkedModelSerializer):
    """
    Profile Serializer
    """
    user = serializers.SlugRelatedField(slug_field='username', queryset=User.objects.all())
    module = serializers.SlugRelatedField(slug_field='id', queryset=AttendeeModule.objects.all())
    picture = serializers.ImageField(allow_empty_file=False, required=False)

    class Meta:
        model = AttendeeProfile
        fields = (
            'user', 'module', 'title', 'company', 'email', 'picture', 'is_active', 'created'
        )

我想过滤模块字段,la serializers.SlugRelatedField(slug_field='id', queryset=AttendeeModule.objects.filter(module__app='module__app')或类似的东西,每个模块属于一个应用程序。

我想我需要通过视图为序列化器提供更多上下文?我应该覆盖视图上的方法吗?我已经尝试了但是我还是Django休息框架的新手,它给了我很多麻烦

视图是

class AttendeesList(generics.ListCreateAPIView):
    """

    """
    queryset = AttendeeProfile.objects.all()
    serializer_class = AttendeeProfileSerializer

并使用此路线

url(r'^apps/(?P<url_name>[a-z][a-z0-9]+)/modules/(?P<module_id>[0-9]+)/attendees$',
    views.AttendeesList.as_view(),
    name='attendees-list'),

3 个答案:

答案 0 :(得分:2)

就我而言,我还需要访问request.user进行过滤,上面没有解决方案,因此下面是示例代码:

class MyFieldName(serializers.SlugRelatedField):
    def get_queryset(self):
        queryset = MyModel.objects.all()
        request = self.context.get('request', None)
        if not request.user.is_superuser:
            queryset = queryset.filter(user=request.user)
        return queryset

class MySerializer(serializers.ModelSerializer):
    organization = MyFieldName(slug_field='slug')

答案 1 :(得分:1)

所以我找到了一个解决我古老问题的方法:

class SlugRelatedModuleField(SlugRelatedField):

    def get_queryset(self):
        queryset = self.queryset
        if hasattr(self.root, 'app_id'):
            queryset = queryset.filter(module__app_id=self.root.app_id)
        return queryset

class AttendeeProfileSerializer(ModelSerializer):

    def __init__(self, *args, **kwargs):
        self.app_id = kwargs.pop('app_id')
        super().__init__(*args, **kwargs)

    module = SlugRelatedModuleField(
        slug_field='id', 
        queryset=AttendeeModule.objects.all()
    )

    class Meta:
        model = AttendeeProfile
        fields = ('user', 'module', 'title', 'company', 'email', 'picture', 'is_active', 'created')

这会在父/根序列化程序上分配app_id属性,而SlugRelatedModuleField会检查父级以过滤查询集。多田。

答案 2 :(得分:0)

我正在使用序列化程序的 context parameter

class SlugRelatedModuleField(SlugRelatedField):

    def get_queryset(self):
        return self.queryset.filter(module__app_id=self.context["app_id"])

可以像这样在视图(集)中设置上下文:

class AttendeeProfileViewSet(viewsets.ModelViewSet):
    queryset = AttendeeProfile.objects.all()
    serializer_class = AttendeeProfileSerializer

    def get_serializer_context(self) -> Dict[str, Any]:
        return {
            **super().get_serializer_context(),
            "app_id": self.request.data.get("app_id")
        }