rest_framework_mongoengine产生“无效数据。需要字典,但得到了str。”例外

时间:2018-07-28 20:33:09

标签: python django django-rest-framework restframeworkmongoengine

我一直在使用mongoengine框架,并且遇到了上述异常。好像我缺少什么,这就是我想澄清的,希望在您的帮助下。

这是我的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
        )

这是我要发布的json对象:

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

但是它不起作用,我对所有这些都不了解吗?

编辑

这是服务器回溯:

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 个答案:

没有答案