Django Rest Framework - filtering nested serializer

Viewed 1624

One Book can have many BookContent in different languages. In my example, the Book has 2 BookContents, one in english and the other in chinese.

I am getting the Book and its content in a specific language by specifying the language queryset like this : http://localhost/api/books/?language=english. When this is call, it calls BookList get_query in views.py where I filter the content by language. However, the json results still print all the BookContent for that Book.

Below are my codes.

models.py

class Book(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(max_length=255)

class BookContent(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    book = models.ForeignKey(Book, releated_name='content' on_delete=models.CASCADE)
    content = models.TextField()
    language = models.TextField()

serializers.py

class BookContentSerializer(serializers.ModelSerializer):
    class Meta:
        model = BookContent
        fields = ('id', 'content', 'language')

class BookSerializer(serializers.ModelSerializer):
    content = BookContentSerializer(many=True, read_only=True)

    class Meta:
        model = Book
        fields = ('id', 'title', 'content')

views.py

class BookList(generics.ListAPIView):
    serializer_class = BookSerializer
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        if 'language' in self.request.query_params:
            language = self.request.query_params['language']
        return Book.objects.filter(content__language=language).distinct().order_by('title')

class BookDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
    permission_classes = (permissions.IsAuthenticated,)

Current Json results:

{
    "results": [
        {
            "id": "d3e5185a-1b7b-427c-bbe3-030bfa2e3bce",
            "title": "My Book Title",            
            "book_content": [
                {
                    "id": "0fea8027-3ecf-4571-a95f-5a09a93408ec",
                    "content": "hello content 1",
                    "language": "english"
                },
                {
                    "id": "0fea8027-3ecf-4571-a95f-5a09a93408ed",
                    "content": "你好",
                    "language": "chinese"
                }
            ]
        }
    ]
}

How do I get a list of Books and the BookContent language for the specific language only?

Expected Json results:

{
        "results": [
            {
                "id": "d3e5185a-1b7b-427c-bbe3-030bfa2e3bce",
                "title": "My Book Title",            
                "book_content":
                    {
                        "id": "0fea8027-3ecf-4571-a95f-5a09a93408ec",
                        "content": "hello content 1",
                        "language": "english"
                    }

            }
        ]
    }
0 Answers
Related