17

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

2
  • 3
    Could you paste the full traceback ? Commented Mar 20, 2017 at 13:20
  • Something is probably missing in your view. Are you using viewsets ? generic views? Commented Mar 20, 2017 at 22:40

5 Answers 5

49

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!

Sign up to request clarification or add additional context in comments.

Comments

4

Please check your indentation, that 'create' function should be inside that Serializer class, as below example.

class StudentSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=111)
    roll = serializers.IntegerField()
    city = serializers.CharField(max_length=722)

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

Comments

-1

Check if the keys inside data exactly match model attributes when you try to create an object of a particular model using the model serializer

Comments

-1

Please check your indentation, that 'create' function should be within that Serializer class Hopefully it will work.

1 Comment

from rest_framework import serializers from .models import Student class StudentSerializer(serializers.Serializer): name=serializers.CharField(max_length=555) age=serializers.CharField(max_length=555) city=serializers.CharField(max_length=555) def create(self,validated_data): return Student.objects.create(**validated_data)
-1

from rest_framework import serializers from .models import Student

class StudentSerializer(serializers.Serializer): name=serializers.CharField(max_length=555) def create(self,validated_data): return Student.objects.create(**validated_data)enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.