如何在Django Rest Framework中过滤嵌套的序列化? [英] How do you filter a nested serializer in Django Rest Framework?
问题描述
在Django Rest Framework中,如何将序列化程序嵌套在另一个序列化程序中,如何过滤?
In Django Rest Framework, how do you filter a serializer when it's nested in another serializer?
我的过滤器被强加在DRF视图中,但是当您调用串行器从另一个序列化程序中,嵌套的序列化程序的视图从未被调用,所以嵌套的结果显示为未过滤。
My filters are imposed in the DRF viewsets, but when you call a serializer from inside another serializer, the viewset of the nested serializer never gets called, so the nested results appear unfiltered.
我尝试在原始视图中添加过滤器,但是似乎没有过滤嵌套的结果,因为嵌套的结果被调用为一个单独的pre-fretched查询。 (嵌套的序列化程序是一个反向查找,你会看到。)
I have tried adding a filter on originating viewset, but it doesn't seem to filter the nested results because the nested results get called as a separate pre-fretched query. (The nested serializer is a reverse lookup, you see.)
是否可以在嵌套的序列化程序本身中添加一个get_queryset()覆盖(将其从视图中移出) ),添加过滤器在那里?我也尝试过,没有运气。
Is it possible to add a get_queryset() override in the nested serializer itself (moving it out of the viewset), to add the filter there? I've tried that, too, with no luck.
这是我试过的,但似乎没有被调用:
This is what I tried, but it doesn't even seem to get called:
class QuestionnaireSerializer(serializers.ModelSerializer):
edition = EditionSerializer(read_only=True)
company = serializers.StringRelatedField(read_only=True)
class Meta:
model = Questionnaire
def get_queryset(self):
query = super(QuestionnaireSerializer, self).get_queryset(instance)
if not self.request.user.is_staff:
query = query.filter(user=self.request.user, edition__hide=False)
return query
任何帮助赞赏
John
推荐答案
您可以对 ListSerializer 并覆盖 to_representation
方法。
You can subclass the ListSerializer and overwrite the to_representation
method.
默认情况下, to_representation
方法调用 data.all()
嵌套查询集。所以在调用该方法之前,您有效地需要使 data = data.filter(** your_filters)
。然后,您需要在嵌套序列化程序的元素上添加子类ListSerializer作为list_serializer_class。
By default the to_representation
method calls data.all()
on the nested queryset. So you effectively need to make data = data.filter(**your_filters)
before the method is called. Then you need to add your subclassed ListSerializer as the list_serializer_class on the meta of the nested serializer.
- 子类ListSerializer,覆盖
to_representation
然后调用super - 将子类化的ListSerializer添加到嵌套的Serializer上的元
list_serializer_class
li>
- subclass ListSerializer, overwriting
to_representation
and then calling super - add subclassed ListSerializer as the meta
list_serializer_class
on the nested Serializer
以下是您样本的相关代码。
Here is the relevant code for your sample.
class FilteredListSerializer(serializers.ListSerializer):
def to_representation(self, data):
data = data.filter(user=self.request.user, edition__hide=False)
return super(FilteredListSerializer, self).to_representation(data)
class EditionSerializer(serializers.ModelSerializer):
class Meta:
list_serializer_class = FilteredListSerializer
model = Edition
class QuestionnaireSerializer(serializers.ModelSerializer):
edition = EditionSerializer(read_only=True)
company = serializers.StringRelatedField(read_only=True)
class Meta:
model = Questionnaire
这篇关于如何在Django Rest Framework中过滤嵌套的序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!