【问题标题】:Tastypie foreign Key relationship 'dict' object has no attribute 'obj'Tastypie外键关系'dict'对象没有属性'obj'
【发布时间】:2013-10-02 10:00:44
【问题描述】:

我遇到了美味派的问题,但我找不到是什么原因造成的。没有答案的类似问题:Tastypie foreign key relationship throwing error

资源:

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

让我感到沮丧的是,它就像 2 天前一样工作。我添加了 1 个其他资源,而没有触及选择、用户或与模型相关的任何其他资源。我检查了最近的提交历史,并且资源没有受到影响。

【问题讨论】:

    标签: python django tastypie


    【解决方案1】:

    通过美味的源代码调试我的方式并解决了我的问题。

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

    当 sweetpie 脱水 choice 时,它显然没有得到一个 bundle 对象,因此没有 obj

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 2017-06-14
      • 1970-01-01
      • 2017-03-30
      • 2012-01-12
      • 2021-03-08
      • 2017-05-12
      • 2019-03-11
      • 1970-01-01
      相关资源
      最近更新 更多