0

REST Frameworkを使用して、POST を作成すると、次のエラーが発生します...

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

PUT は問題なく動作しているようです。

シリアライザ

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)
4

1 に答える 1

3

あなたのメソッドは、コンストラクターrestore_objectに誤って渡そうとしています。attribute_answersProfile

たまたま、使用しているModelSerializerため、そのメソッドはまったく必要ありませんrestore_object。モデル インスタンスの復元が処理されます。このメソッドは、基本クラスrestore_objectにのみ必要です。Serializer

于 2013-02-20T10:38:01.350 に答える