本文介绍了过滤嵌套序列化程序 - Django REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 models.py:

类问题(models.Model):代码=models.CharField(max_length=12)text = models.CharField(max_length=1000, null=True)catgeroy = models.ForeignKey(类别,on_delete=models.PROTECT,null=True,空白=True,db_index=True,related_name='category')类答案(模型.模型):问题=models.ForeignKey(问题,on_delete=models.PROTECT,null=True,空白=True,db_index=True,related_name='question')考试=models.ForeignKey(考试,on_delete=models.CASCADE)value = models.FloatField(null=True, blank=True)类考试(模型.模型):年份 = models.IntegerField()

我嵌套的 serializer 看起来像这样:

类 AnswerSerializer(serializers.ModelSerializer):related_name = '答案'元类:模型 = 价值字段 = ('id', 'value', 'question', 'exam')类 NestedQuestionAnswerSerializer(serializers.ModelSerializer):answer = AnswerSerializer(many=True, read_only=True)元类:模型 = 问题字段 = ('id'、'code'、'text'、'answer')

我的 views.py 看起来像这样:

class QuestionAnswerViewSet(BaseCertViewSet):查询集 = Question.objects.all()serializer_class = serializers.NestedQuestionAnswerSerializerfilter_backends = [filters.DjangoFilterBackend]filterset_fields = ('类别',)

我的 urls.py 看起来像这样:

router.register('问题-答案', views.QuestionAnswerViewSet, '问题-答案')

我希望能够按类别和考试进行过滤(这是一个子属性).所以是这样的:https://example.com/api/question-answer?category=4&exam=21

这可能应该返回属于 category=4 并且出现在考试=21 中的所有问题.

我单独通过 category 过滤没有问题,但似乎无法过滤 exam 这是一个子外键.

我在 SO 上尝试了很多解决方案,但似乎没有一个能做到以上几点.

更新:

感谢大家提出的解决方案.

我最终使用了这个解决方案

添加了一个List Serializer类并修改了to_representation函数:

class FilteredAnswerSerializer(serializers.ListSerializer):def to_representation(self, data):qry_exam = self.context['request'].GET.get('exam')数据 = data.filter(exam=qry_exam)返回 super(FilteredAnswerSerializer, self).to_representation(data)

然后在我的答案序列化器中我称之为:

类 AnswerSerializer(serializers.ModelSerializer):related_name = '答案'元类:模型 = 价值list_serializer_class = FilteredAnswerSerializer字段 = ('id', 'value', 'question', 'exam')
解决方案

感谢大家提出的解决方案.

我最终使用了这个解决方案

添加了一个List Serializer类并修改了to_representation函数:

class FilteredAnswerSerializer(serializers.ListSerializer):def to_representation(self, data):qry_exam = self.context['request'].GET.get('exam')数据 = data.filter(exam=qry_exam)返回 super(FilteredAnswerSerializer, self).to_representation(data)

然后在我的答案序列化器中我称之为:

类 AnswerSerializer(serializers.ModelSerializer):related_name = '答案'元类:模型 = 价值list_serializer_class = FilteredAnswerSerializer字段 = ('id', 'value', 'question', 'exam')

我会用解决方案更新我的问题.

I have the following models.py:

class Question(models.Model):

    code = models.CharField(max_length=12)
    text = models.CharField(max_length=1000, null=True)
    catgeroy = models.ForeignKey(
        Category, on_delete=models.PROTECT, null=True, blank=True, db_index=True, related_name='category')


class Answer(models.Model):

    question = models.ForeignKey(
        Question, on_delete=models.PROTECT, null=True, blank=True, db_index=True, related_name='question')
    exam = models.ForeignKey(Exam, on_delete=models.CASCADE)
    value = models.FloatField(null=True, blank=True)


class Exam(models.Model):

    year = models.IntegerField()

my nested serializer looks like this:


class AnswerSerializer(serializers.ModelSerializer):

    related_name = 'answer'

    class Meta:
        model = Value
        fields = ('id', 'value', 'question', 'exam')


class NestedQuestionAnswerSerializer(serializers.ModelSerializer):

    answer = AnswerSerializer(many=True, read_only=True)

    class Meta:
        model = Question
        fields = (
            'id','code', 'text', 'answer'
        )

my views.py looks like this:

class QuestionAnswerViewSet(BaseCertViewSet):
    queryset = Question.objects.all()
    serializer_class = serializers.NestedQuestionAnswerSerializer
    filter_backends = [filters.DjangoFilterBackend]
    filterset_fields = ('category',)

my urls.py looks like this:

router.register('question-answer', views.QuestionAnswerViewSet, 'question-answer')

What I would like to be able to do is to filter by both Category AND Exam(which is a child attribute). So something like this: https://example.com/api/question-answer?category=4&exam=21

This potentially should return all the Questions that are part of category=4 AND appeared on exam=21.

I have no problem filtering by category alone, but can't seem to filter on exam which is a child foreign key.

I've tried many solutions on SO but none seem to do the above.

UPDATE:

Thanks everyone for your suggested solutions.

I ended up using this solution

Added a List Serializer class and modified the to_representation function:

class FilteredAnswerSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        qry_exam = self.context['request'].GET.get('exam')
        data = data.filter(exam=qry_exam)
        return super(FilteredAnswerSerializer,  self).to_representation(data)

and then in my Answer serializer I call it:


class AnswerSerializer(serializers.ModelSerializer):

    related_name = 'answer'

    class Meta:
        model = Value

        list_serializer_class = FilteredAnswerSerializer

        fields = ('id', 'value', 'question', 'exam')
解决方案

Thanks everyone for your suggested solutions.

I ended up using this solution

Added a List Serializer class and modified the to_representation function:

class FilteredAnswerSerializer(serializers.ListSerializer):
    def to_representation(self, data):
        qry_exam = self.context['request'].GET.get('exam')
        data = data.filter(exam=qry_exam)
        return super(FilteredAnswerSerializer,  self).to_representation(data)

and then in my Answer serializer I call it:


class AnswerSerializer(serializers.ModelSerializer):

    related_name = 'answer'

    class Meta:
        model = Value

        list_serializer_class = FilteredAnswerSerializer

        fields = ('id', 'value', 'question', 'exam')

I will update my question with the solution.

这篇关于过滤嵌套序列化程序 - Django REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 04:14