我正在尝试创建REST API,但我面临以下问题:
/tastypie/resources.py“,第1475行,在get_object_list中 return self._meta.queryset._clone()
AttributeError:'NoneType'对象没有属性'_clone'
class ManageResource(ModelResource):
class Meta(ModelResource.Meta):
resource_name = 'resourceStatus'
detail_allowed_methods = ['get']
always_return_data = True
default_format = 'application/json'
def base_urls(self):
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name,
trailing_slash()), self.wrap_view('dispatch_list'),
name="api_dispatch_list"),
url(r"^(?P<resource_name>%s)/(?P<request_id>[\w\d_.-]+)%s$"
% (self._meta.resource_name, trailing_slash()),
self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
def get_detail(self, request, **kwargs):
id = int(kwargs["request_id"])
return self._get_response(request, self.obj_get(request=request,id=id))
@obj_wrapper
def obj_get(self, request, id):
result = Helper().status(id=id)
return {"info":{"start_time":result.get("start_time"),\
"end_time":result.get("end_time"),\
"status":result.get("status"),\
"messages":result.get("messages")}}
我没有在元类中设置queryset,因为我从文件或某种静态资源中提取数据。
答案 0 :(得分:2)
将queryset
字段添加到资源的Meta
。如果您未使用标准QuerySet
,请覆盖get_object_list
方法。
答案 1 :(得分:0)
以下是我如何做到这一点:
class SomeFakedResource(Resource):
# list out your fields as usual
test_quantity = fields.IntegerField(attribute='test_quantity')
class Meta:
resource_name = 'some_faked'
allowed_methods = ['get']
include_resource_uri = False
def _calculate_test_quantity(self):
# here's where I put the real work of calculating the field
return 1
def obj_get_list(self, bundle, **kwargs):
return self.get_object_list(bundle.request)
def get_object_list(self, request):
self.test_quantity = self._calculate_test_quantity()
return [self]