我使用 django、 django-rest-restframwork、django-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)
有任何想法吗?