title: Filter on Nested serializer - Django REST
body: <p>I have the following <code>models.py</code>:</p>
  
  <pre><code>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()
  
  </code></pre>
  
  <p>my nested <code>serializer</code> looks like this:</p>
  
  <pre><code>
  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'
          )
  
  </code></pre>
  
  <p>my <code>views.py</code> looks like this:</p>
  
  <pre><code>class QuestionAnswerViewSet(BaseCertViewSet):
      queryset = Question.objects.all()
      serializer_class = serializers.NestedQuestionAnswerSerializer
      filter_backends = [filters.DjangoFilterBackend]
      filterset_fields = ('category',)
  </code></pre>
  
  <p>my <code>urls.py</code> looks like this:</p>
  
  <pre><code>router.register('question-answer', views.QuestionAnswerViewSet, 'question-answer')
  </code></pre>
  
  <p><strong>What I would like to be able to do is to filter by both Category AND Exam</strong>(which is a child attribute). So something like this: <code>https://example.com/api/question-answer?category=4&amp;exam=21</code></p>
  
  <p>This potentially <em>should</em> return all the Questions that are part of category=4 AND appeared on exam=21.</p>
  
  <p>I have no problem filtering by <code>category</code> alone, but can't seem to filter on <code>exam</code> which is a child foreign key.</p>
  
  <p>I've tried many solutions on SO but none seem to do the above.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Thanks everyone for your suggested solutions.</p>
  
  <p>I ended up using <a href="https://stackoverflow.com/questions/42156901/filtered-nested-relationships-django-rest-framework">this solution</a></p>
  
  <p>Added a List Serializer class and modified the <code>to_representation</code> function:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>and then in my Answer serializer I call it: </p>
  
  <pre><code>
  class AnswerSerializer(serializers.ModelSerializer):
  
      related_name = 'answer'
  
      class Meta:
          model = Value
  
          list_serializer_class = FilteredAnswerSerializer
  
          fields = ('id', 'value', 'question', 'exam')
  </code></pre>