【问题标题】:AttributeError: 'list' object has no attribute 'model' TastyPieAttributeError:“列表”对象没有属性“模型”TastyPie
【发布时间】:2014-04-01 17:37:57
【问题描述】:

我不断收到以下错误:

Traceback (most recent call last):

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 217, in wrapper
    response = callback(request, *args, **kwargs)

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 459, in dispatch_list
    return self.dispatch('list', request, **kwargs)

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 491, in dispatch
    response = method(request, **kwargs)

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 1299, in get_list
    objects = self.obj_get_list(bundle=base_bundle, **self.remove_api_resource_names(kwargs))

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 2113, in obj_get_list
    return self.authorized_read_list(objects, bundle)

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/resources.py", line 610, in authorized_read_list
    auth_result = self._meta.authorization.read_list(object_list, bundle)

  File "/srv/www/poka/app/env/main/lib/python2.7/site-packages/tastypie/authorization.py", line 151, in read_list
    klass = self.base_checks(bundle.request, object_list.model)

AttributeError: 'list' object has no attribute 'model'

当我调用以下模型时会发生这种情况:

class NewsResource(ModelResource):
    class Meta:

        queryset = News.objects.select_related('picture').all()
        allowed_methods = ['get','patch']
        include_resource_uri = False
        include_absolute_url = False
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()

有什么想法吗?

【问题讨论】:

    标签: python django rest tastypie


    【解决方案1】:

    下面是一个例子来证明你的错误:

    >>> object_list = [1, 2, 3]
    >>> object_list.model
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
    AttributeError: 'list' object has no attribute 'model'
    

    我怀疑在您的情况下object_list 是一个列表,因此没有属性model。请检查您的代码。

    【讨论】:

    • 所以.. 我发现在我的函数 def apply_filters(self, request, apply_filters): 中,如果它没有我的自定义过滤器,我会返回 []。结果,当 Tastypie 在我的 [] 上做一个 .model 时,它找不到模型。我该如何解决这个问题?
    • 那么apply_filters() 总是返回一个列表吗?当没有自定义过滤器时,它返回[]。否则它会返回什么?
    • 返回一个列表,但附加了一个“模型”对象。所以我只需要这样做:News.objects.none() 而不是 []。
    • 你需要重新设计你的代码来检查空列表。
    【解决方案2】:

    正如 Hai vu 所说,您的对象没有 model 属性。

    为了帮助您了解您的问题,您可以使用 PDB 进行调试。 这很容易设置。 就在负责问题的那一行之前,在你的代码中写下:

    import pdb; pdb.set_trace()

    它会冻结你的服务器作为代码中的这一点。 然后在 shell 中,随意测试以下内容:

    list.model # will throw the same error
    

    list.__dict__ # will show all the possible attributes that you can use with the list object
    

    【讨论】:

      猜你喜欢
      • 2018-06-12
      • 2015-04-28
      • 2021-10-20
      • 2019-05-11
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      相关资源
      最近更新 更多