2013-10-02 59 views
1

我遇到了tastypie的问题,我无法找到是什么原因造成的。无答案类似的问题:Tastypie foreign key relationship throwing errorTastypie外键关系'字典'对象没有属性'obj'

资源:

class VoteResource(ModelResource): 
    choice = fields.ToOneField(ChoiceResource, 'choice', full=True) 
    user = fields.ToOneField(UserResource, 'user') 

    class Meta: 
     queryset = Vote.objects.all() 
     resource_name = 'vote' 
     '''...''' 
     always_return_data = True 
     filtering = { 
      'id': ALL, 
      'user': ALL_WITH_RELATIONS, 
      'choice': ALL_WITH_RELATIONS 
     } 

    def hydrate(self, bundle):  
     bundle.obj.user = bundle.request.user 
     return bundle 

用于创建对象请求有效载荷:

{ 
    "choice": "/api/v1/choice/210/" 
} 

(用户正在越来越通过水合物自动添加)。在full_hydrate的ressources.py中抛出异常。根据Django控制台我的对象正在正确加载。

内tastypie线造成这是

setattr(bundle.obj, field_object.attribute, value.obj) # value obj is the evil one 

Tastypie sourcecode here

什么杀了我的,它的工作就像3天前。我添加了1个其他资源,但未触及选择,用户或与该模型相关的任何其他资源。我检查了最近的提交历史记录,并且资源未被触及。

回答

1

调试我的方式通过tastypie源并解决了我的问题。

看起来像tastypie首先调用相关对象的脱水。由于误解,我在choice的脱水处理中返回了包中的数据,而不是实际的包本身。

当tastypie脱水choice,它显然没有得到捆绑对象,因此没有obj

相关问题