why django-rest-framework-mongoengine-serializers is_valid() always return false

1.9k views Asked by At

I build a RESTful API with django,django-rest-restframwork,django-rest-framework-mongoengine,mongodb,mongoengine.Everything goes right but create or update a document.When validating a document using serializer.is_valid(),serializer always return false.here is my codes below:

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)

Any ideas?

1

There are 1 answers

0
Abhishek Sharma On

Check your Django Version it must be grater then 1.6 install latest version, in your old rest_framework it uses in your post method data=request.DATA so thats way its giving Allays FALSE

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