0

I am using Django 2.2, MongoDb with Djongo. I am facing issues in POST and PATCH API. I am facing three kinds of issues.

  1. When performing a POST operation to create a new entry, API fails with error: Array items must be Model instances.
  2. What is the correct way to refer an instance of Screenplay class in POST API. Is the id of the parent class sufficient?
  3. How to perform a update to a specific field in Scene model including a text field in comments?

Following is the code snippet.

Sample POST API data

{
    "title": "intro1",
    "screenplay": "49423c83-0078-4de1-901c-f9176b51fd33",
    "comments": [
        {
            "text": "hello world",
            "author": "director"
        }
    ]
}

models.py

import uuid
from djongo import models

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

    def __str__(self):
        return self.title

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

    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)
    screenplay = models.ForeignKey(Screenplay, related_name='scenes', on_delete=models.CASCADE)

    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 ScreenplaySerializer(serializers.ModelSerializer):

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

class CommentSerializer(serializers.HyperlinkedModelSerializer):

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

class SceneSerializer(serializers.HyperlinkedModelSerializer):
    comments = CommentSerializer();

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

viewsets.py

from planning.models import Screenplay, Scene, Comment
from .serializers import  ScreenplaySerializer, 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 ScreenplayViewSet(viewsets.ModelViewSet):
    queryset = Screenplay.objects.all()
    serializer_class = ScreenplaySerializer

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

class CommentViewSet(viewsets.ModelViewSet):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer
antman
  • 317
  • 6
  • 16

1 Answers1

0

I suggest you read the documentation on Writable nested representations, it will help to dissipate your doubts.

luistm
  • 1,027
  • 4
  • 18
  • 43