【问题标题】:Tastypie: getting "AttributeError: 'NoneType' object has no attribute '_clone'" ErrorTastypie:得到“AttributeError:'NoneType'对象没有属性'_clone'”错误
【发布时间】:2017-08-17 18:36:17
【问题描述】:

当我尝试向我的模型发出 GET(使用 TastyPie)时,我收到以下回溯错误:

File "/Library/Python/2.7/site-packages/tastypie/resources.py", line 
2141, in get_object_list
return self._meta.queryset._clone()
AttributeError: 'NoneType' object has no attribute '_clone'

下面是我的resources.py 文件,用于相关模型。

from tastypie.resources import ModelResource
from swtr.models import Com

class ComResource(ModelResource):
    class Meta:
        query_set = Com.objects.all()
        resource_name = 'com'
        object_class = none

考虑到Com.objects.all() 返回至少一条我在python shell 中创建并保存的记录,我感到特别困惑。所以我不确定为什么查询集被返回为NoneType

【问题讨论】:

  • 包括完整的回溯和导致此错误的视图
  • 您确定在您的ComResource 的元类中是query_set 而不是queryset
  • @wencakisa 哇——你是对的——它是查询集!我不知道在翻阅文档时我是怎么错过的……非常感谢您为我发现了这一点。

标签: python django tastypie


【解决方案1】:

您在ComResourceMeta 类中拼错了一些字段。

尝试将query_set 重命名为queryset 并将object_class 的值设为None,而不是none

from tastypie.resources import ModelResource
from swtr.models import Com


class ComResource(ModelResource):
    class Meta:
        queryset = Com.objects.all()
        # __^
        resource_name = 'com'
        object_class = None
        # _____________^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2017-03-10
    • 2019-07-18
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多