django-rest-framework: Cannot call `.is_valid()` as no `data=` keyword argument was passed when instantiating the serializer instance

后端 未结 1 1425
情话喂你
情话喂你 2020-12-29 05:31

I\'ve the following model:

class NoteCategory(models.Model):
    title = models.CharField(max_length=100, unique=True)

    def __unicode__(self):
        re         


        
相关标签:
1条回答
  • 2020-12-29 05:54

    When you want to serialize objects, you pass object as a first argument.

    serializer = CommentSerializer(comment)
    serializer.data
    # {'email': u'leila@example.com', 'content': u'foo bar', 'created': datetime.datetime(2012, 8, 22, 16, 20, 9, 822774)}
    

    But when you want to deserialize you pass the data with a data kwarg.

    serializer = CommentSerializer(data=data)
    serializer.is_valid()
    # True
    serializer.validated_data
    # {'content': 'foo bar', 'email': 'leila@example.com', 'created': datetime.datetime(2012, 08, 22, 16, 20, 09, 822243)}
    

    So in your case you want to deserialize your post data, therefor you have to do:

    serializer = PatientNoteSerializer(data=request.data)
    
    0 讨论(0)
提交回复
热议问题