2013-02-19 64 views
0

使用REST Framework,使得POST我收到以下錯誤,當無效的關鍵字參數...Django的REST:是此功能

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) 
+0

我懷疑,因爲當你'POST',你正在創建一個新的項目,你還沒有相關的領域。 – 2013-02-19 18:25:32

回答

3

restore_object方法錯誤地試圖通過attribute_answersProfile構造。

碰巧,由於您使用的是ModelSerializer,所以根本不需要restore_object方法 - 將爲您處理模型實例恢復。 restore_object方法僅適用於基本的Serializer類。