rest_framework_mongoengine yields the "Invalid data. Expected a dictionary, but got str." exception

333 views Asked by At

I've been playing around with the mongoengine framework and came across the above mentioned exception. Seems like I'm missing something and that's what I would like to clarify, hopefully with your help.

Here is my MSV:

models.py

import mongoengine


class PersonAddressModel(mongoengine.DynamicEmbeddedDocument):
    country = mongoengine.fields.StringField()
    town = mongoengine.fields.StringField()


class PersonInfoModel(mongoengine.DynamicDocument):
    name = mongoengine.fields.StringField()
    age = mongoengine.fields.IntField()
    is_married = mongoengine.fields.BooleanField()
    address = mongoengine.fields.MapField(
        mongoengine.fields.EmbeddedDocumentField(
            PersonAddressModel
        )
    )

serializers.py

from rest_framework_mongoengine import serializers
from .models import PersonInfoModel, PersonAddressModel


class PersonAddressSerializer(serializers.EmbeddedDocumentSerializer):

    class Meta:
        model = PersonAddressModel
        fields = '__all__'


class PersonInfoSerializer(serializers.DocumentSerializer):

    def create(self, validated_data):
        return PersonInfoModel.objects.create(**validated_data)

    class Meta:
        model = PersonInfoModel
        fields = '__all__'

views.py

from rest_framework_mongoengine import viewsets
from .serializers import PersonInfoSerializer
from rest_framework.response import Response
from rest_framework import status
import djongo


class PersonInfoView(viewsets.ModelViewSet):
    lookup_field = 'id'
    serializer_class = PersonInfoSerializer

    def create(self, request):
        serializer = self.serializer_class(data=request.data)

        try:
            serializer.is_valid(raise_exception=True)
            serializer.save()
        except djongo.sql2mongo.SQLDecodeError:
            return Response(
                status=status.HTTP_503_SERVICE_UNAVAILABLE
            )

        return Response(
            status=status.HTTP_201_CREATED
        )

Here is the json object I'm trying to post:

{
    "name": "John",
    "age": 20,
    "is_married": false,
    "address": {
        "country": "Canada",
        "town": "Toronto"
    }
}

But it doesn't work, what am I failing to understand about all this?

EDIT

Here is the server traceback:

Internal Server Error: /api/v1/person
Traceback (most recent call last):
  File "/home/dop/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/home/dop/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/dop/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/dop/.local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/viewsets.py", line 95, in view
    return self.dispatch(request, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py", line 494, in dispatch
    response = self.handle_exception(exc)
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py", line 454, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/home/dop/Documents/projects/serverside/essences/person/views.py", line 17, in create
    serializer.save()
  File "/usr/local/lib/python3.6/dist-packages/rest_framework/serializers.py", line 184, in save
    'You cannot call `.save()` on a serializer with invalid data.'
AssertionError: You cannot call `.save()` on a serializer with invalid data.
0

There are 0 answers