属性错误:'list'对象没有'model'属性 TastyPie

0 投票
2 回答
4492 浏览
提问于 2025-04-18 00:53

我一直遇到以下错误:

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

有没有什么想法?

2 个回答

1

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

为了帮助你理解这个问题,你可以使用PDB来调试。这非常简单,只需在出问题的那一行代码之前,写上这一行:

import pdb; pdb.set_trace()

这样做会让你的服务器在这段代码处暂停。然后在命令行中,你可以随意测试一些东西,比如:

list.model # will throw the same error

或者

list.__dict__ # will show all the possible attributes that you can use with the list object
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 这个属性。请检查一下你的代码。

撰写回答