Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework: 'create( )' NotImplementedError when making Http POST request

When making a post request in Django I get the error that 'create()' has not been implemented when I did indeed implement it in my serializer file

from rest_framework import serializers
from people.models import People

class PeopleSerializer(serializers.Serializer):
    pk = serializers.IntegerField(read_only=True)
    name = serializers.CharField(max_length=200)
    favoriteCity = serializers.CharField(max_length=200)

    def create(self, validated_data):
        return People.objects.create(**validated_data)

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.favoriteCity = validated_data.get(
                                'favoriteCity',instance.favoriteCity)
        instance.save()
        return instance()

Clearly the create method has been implemented and I don't understand why i'm getting a NotImplementedError

like image 255
Mutating Algorithm Avatar asked Mar 20 '17 13:03

Mutating Algorithm


1 Answers

In your serializer class inherit from ModelSerializer instead of Serializer class since the later doesn't call create() method implicitly.

class PeopleSerializer(serializers.ModelSerializer): 

The above change would do your job!

like image 96
dushyant7917 Avatar answered Sep 22 '22 08:09

dushyant7917