Search code examples
djangodjango-rest-frameworkdjongo

Serialize ArrayModelField into JSON in Django DRF


I am using Djongo with Django 2.2. I am using MongoDb and using Djongo. i am facing issues in GET API. The Comment model is not serialized. Following is the code snippet.

models.py

import uuid
from djongo import models

class Comment(models.Model):
    text = models.TextField();
    author = models.TextField();

    class Meta:
        abstract = True

    def __str__(self):
        return self.author +self.text

class Scene(models.Model):
    id = models.UUIDField(primary_key = True, default = uuid.uuid4,editable = False)
    title = models.CharField(max_length=100)
    comments = models.ArrayModelField(
        model_container = Comment,
    );

    def __str__(self):
        return self.title

serializers.py

from rest_framework import serializers
from planning.models import Scene, Comment
class CommentSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Comment
        fields = ('text', 'author')

class SceneSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Scene
        comments = CommentSerializer();
        fields = ('id', 'title', 'comments')

viewsets.py

from planning.models import Scene, Comment
from .serializers import  SceneSerializer, CommentSerializer
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import status
from rest_framework import generics
from rest_framework.generics import RetrieveUpdateDestroyAPIView

class SceneViewSet(viewsets.ModelViewSet):
    queryset = Scene.objects.all()
    serializer_class = SceneSerializer

class CommentViewSet(viewsets.ModelViewSet):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer

Output for GET scene model in DRF

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": "28db3aa8-34ef-4880-a3eb-57643814af22",
        "title": "scene 1",
        "comments": "[<Comment: edwardcomment1>, <Comment: edwardcomment2>]"
    }
]

Expected output

HTTP 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

[
    {
        "id": "28db3aa8-34ef-4880-a3eb-57643814af22",
        "title": "scene 1",
        "comments": "[  { name : edward, text: comment1 }, 
            { name : edward, text: comment2 } ]"
    }
]

The Comment which is ArrayModelField of Djongo is not serialized properly as expected.


Solution

  • Serialize comments outside of Meta:

    from rest_framework import serializers
    from planning.models import Scene, Comment
    
    class CommentSerializer(serializers.ModelSerializer):
    
       class Meta:
          model = Comment
          fields = ('text', 'author')
    
    
    class SceneSerializer(serializers.ModelSerializer):
       comments = CommentSerializer(many=True, read_only=True)
    
       class Meta:
          model = Scene
          fields = ('id', 'title', 'comments')
    

    DRF nested relationships