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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With