0

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

模型.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'}

序列化程序.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

视图.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)

有任何想法吗?

4

1 回答 1

2

检查您的 Django 版本,它必须更高,然后 1.6 安装最新版本,在您的旧 rest_framework 中,它在您的 post 方法中使用,data=request.DATA这样就可以提供 AllaysFALSE

def post(self, request)
    serializer = MySerializers(data=request.data)
于 2015-10-16T08:04:45.490 回答