jason
jason

Reputation: 3075

Django REST:is invalid keyword argument for this function

Using the REST Framework, when making a POST I get the following error...

TypeError at /api/profiles/
'attribute_answers' is an invalid keyword argument for this function

PUT seems to work without any issues.

Serializer

class ProfileSerializer(serializers.ModelSerializer):
    user = serializers.SlugRelatedField(slug_field='username')
    attribute_answers = serializers.PrimaryKeyRelatedField(many=True)

    class Meta:
        model = Profile
        depth = 2
        fields = ('id', 'name', 'active', 'type', 'user', 'attribute_answers')

    def restore_object(self, attrs, instance=None):
        """
        Create or update a new snippet instance.

        """
        if instance:
            # Update existing instance
            instance.name = attrs.get('name', instance.name)
            instance.active = attrs.get('active', instance.active)
            instance.type = attrs.get('type', instance.type)
            instance.attribute_answers = attrs.get('attribute_answers', instance.attribute_answers)
            return instance

        # Create new instance
        return Profile(**attrs)

Upvotes: 0

Views: 2074

Answers (1)

Tom Christie
Tom Christie

Reputation: 33901

Your restore_object method is incorrectly attempting to pass attribute_answers to the Profile constructor.

As it happens, since you're using ModelSerializer you don't need that restore_object method at all - the model instance restore will be handled for you. The restore_object method is only required for basic Serializer classes.

Upvotes: 3

Related Questions