2014-04-01 54 views
0

我不断收到以下错误:AttributeError的:“名单”对象有没有属性“模型” TastyPie

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

任何想法?

回答

0

这是证明你错误的例子:

>>> 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。请检查您的代码。

+0

所以..我发现,在我的功能高清apply_filters(个体经营,要求,applicable_filters):,我如果它没有我的自定义过滤器,则返回[]。因此,当Tastypie在我的[]上做一个.model时,它找不到一个模型。我怎么能解决这个问题? – abisson

+0

那么'apply_filters()总是返回一个列表?当没有自定义过滤器时,它返回'[]'。它会以什么方式返回? –

+0

返回一个列表,但附有一个“模型”objet。所以我只需要做:News.objects.none()而不是[]。 – abisson

1

由于海武说,你的对象没有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 
相关问题