为什么django-rest-framework-mongoengine-serializers is_valid()总是返回false

时间:2015-08-08 02:38:10

标签: django mongodb django-rest-framework mongoengine restframeworkmongoengine

我使用django,django-rest-restframworkdjango-rest-framework-mongoengine,mongodb,mongoengine构建RESTful API。一切正常但创建或更新文档。使用serializer.is_valid验证文档时( ),序列化器总是返回false。我的代码如下:

models.py

from mongoengine import *

class BusinessExternalLinks(EmbeddedDocument):
    name = StringField()
    url = StringField()
class BusinessCardInfo(DynamicDocument):
    QcardUrl = StringField()
    businessCardPic = StringField()
    businessCardPicSet = ListField(StringField())
    className = StringField()
    createDate = StringField()
    # externalLinkList = ListField(StringField())
    externalLinkList = ListField(EmbeddedDocumentField(BusinessExternalLinks))
    merchantCode = StringField()
    merchantHeadPic = StringField()
    merchantNickName = StringField()
    occupation = StringField()
    praiseNumber = IntField()
    professionalExperience = StringField()
    selfSummary = StringField()
    serviceSurpose = StringField()
    telephone = StringField()
    videoUrl = StringField()
    viewNumber = IntField()
    wechatId = StringField()
    meta = {'collection':'BusinessCardInfo'}

serializers.py

from rest_framework_mongoengine.serializers import DynamicDocumentSerializer, DocumentSerializer, EmbeddedDocumentSerializer
from .models import BusinessCardInfo, BusinessExternalLinks

class ExternalLinkListSerializer(EmbeddedDocumentSerializer):
    class Meta:
        model = BusinessExternalLinks

class BusinessCardInfoSerializer(DocumentSerializer):
    externalLinkList = ExternalLinkListSerializer(many=True)
    class Meta:
        model = BusinessCardInfo

    def update(self, instance, validated_data):
        externalLinkList = validated_data.pop('externalLinkList')
        updated_instance = super(BusinessCardInfoSerializer, self).update(instance, validated_data)

        for externallink in externalLinkList:
            updated_instance.externalLinkList.append(BusinessExternalLinks(**externallink))

        # instance._id = validated_data.get('id', instance.id)
        # instance.merchantNickName = validated_data.get('merchantNickName', instance.merchantNickName)
        updated_instance.save(self)
        return updated_instance

views.py

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework import parsers
from rest_framework import renderers
from rest_framework_mongoengine.serializers import DocumentSerializer
from .models import BusinessCardInfo
from .serializers import BusinessCardInfoSerializer
from rest_framework_mongoengine.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from bson import json_util, ObjectId
import json
from mongoengine import *
class BusinessSingleCardView(APIView):
    """
    retrieve single card
    """
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)

    def get(self, request):
        businesssinglecard = BusinessCardInfo.objects(**{ 'merchantCode' : request.GET.get('tkid') })
        # businesssinglecard = BusinessCardInfo.objects.get(**{ 'merchantCode' : request.GET.get('tkid') })
        # businesssinglecard = BusinessCardInfo.objects.get(merchantCode=request.GET.get('oid'))
        serializer = BusinessCardInfoSerializer(businesssinglecard,many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def post(self, request):
        # data = parsers.JSONParser().parse(request)
        businesscardinfo = BusinessCardInfo.objects(**{ 'merchantCode' : request.GET.get('tkid') })
        serializer = BusinessCardInfoSerializer(businesscardinfo, data=request.data)
        serializer.is_valid()
        # serializer.update(BusinessCardInfo ,serializer.validated_data)
        return Response(serializer.is_valid(), status=status.HTTP_200_OK)

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

检查你的Django版本必须更加严格,然后1.6安装最新版本,在你的post_方法data=request.DATA中使用的旧rest_framework中,这样就可以给出Allays FALSE

def post(self, request)
    serializer = MySerializers(data=request.data)