tastypie 查询集值未显示
tastypie queryset values is not being displayed
我有这个 tastypie 资源:
class TagResource_min(ModelResource):
class Meta:
queryset=Question.objects.values('text', 'id')
当我这样做时出现错误:
{
"error_message": "'dict' object has no attribute 'pk'",
"traceback": "
Traceback (most recent call last):
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 219, in wrapper
response = callback(request, *args, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 450, in dispatch_list
return self.dispatch('list', request, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 482, in dispatch
response = method(request, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in get_list
for obj in to_be_serialized[self._meta.collection_name]
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in <listcomp>
for obj in to_be_serialized[self._meta.collection_name]
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 895, in full_dehydrate
data[field_name] = method(bundle)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1068, in dehydrate_resource_uri
return self.get_resource_uri(bundle)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 819, in get_resource_uri
return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 798, in resource_uri_kwargs
kwargs.update(self.detail_uri_kwargs(bundle_or_obj))
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 775, in detail_uri_kwargs
kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj, self._meta.detail_uri_name)
AttributeError: 'dict' object has no attribute 'pk'"
}
它所指的'dict'
一定是Question.objects.values('text', 'id')
得到的queryset。当我登录 shell 并询问 Question.objects.values('text', 'id')
时,我有以下信息:
<QuerySet [{'text': 'Why does capillary action take place?', 'id': 1}, {'text': "If a human brain was connected to a fish's body, how would the humans thoughts change?", 'id': 2},...
我使用 objects.values()
做错了什么?如何使用 objects.values()
?
显示新的查询集
EDIT 当我使用 Question.objects.values()
时,它 return 是一个 dict
的数组,它没有属性 dict.pk
.但是当我做 Question.objects.all()
时,它 return 是一个 ModelResource
类型的数组,当我调用 ModelResource.pk
.
时,它都是 return 一个整数
这些信息并没有多大帮助...哈哈。因为我确信解决方案就在 tastypie 或 django 库中。尽管如此,如果我能想出一种方法在 dict 上附加一个 pk 就可以解决我的问题。
values()
方法returns一个查询集,returns字典而不是模型实例。模型实例具有 pk
属性,但字典没有。 Tastypie 正在尝试访问字典中的 pk
,从而给出错误。您需要使用一种方法 returns 提供模型实例的查询集。
从 Django's documentation 使用 values()
:
when you know you’re only going to need values from a small number of the available fields and you won’t need the functionality of a model instance object.
在您的情况下,您确实需要模型实例对象的功能,pk
属性。
我建议使用 Question.objects.all()
我有这个 tastypie 资源:
class TagResource_min(ModelResource):
class Meta:
queryset=Question.objects.values('text', 'id')
当我这样做时出现错误:
{
"error_message": "'dict' object has no attribute 'pk'",
"traceback": "
Traceback (most recent call last):
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 219, in wrapper
response = callback(request, *args, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 450, in dispatch_list
return self.dispatch('list', request, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 482, in dispatch
response = method(request, **kwargs)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in get_list
for obj in to_be_serialized[self._meta.collection_name]
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1340, in <listcomp>
for obj in to_be_serialized[self._meta.collection_name]
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 895, in full_dehydrate
data[field_name] = method(bundle)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 1068, in dehydrate_resource_uri
return self.get_resource_uri(bundle)
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 819, in get_resource_uri
return self._build_reverse_url(url_name, kwargs=self.resource_uri_kwargs(bundle_or_obj))
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 798, in resource_uri_kwargs
kwargs.update(self.detail_uri_kwargs(bundle_or_obj))
File \"/home/will/agora/env/lib/python3.5/site-packages/tastypie/resources.py\", line 775, in detail_uri_kwargs
kwargs[self._meta.detail_uri_name] = getattr(bundle_or_obj, self._meta.detail_uri_name)
AttributeError: 'dict' object has no attribute 'pk'"
}
它所指的'dict'
一定是Question.objects.values('text', 'id')
得到的queryset。当我登录 shell 并询问 Question.objects.values('text', 'id')
时,我有以下信息:
<QuerySet [{'text': 'Why does capillary action take place?', 'id': 1}, {'text': "If a human brain was connected to a fish's body, how would the humans thoughts change?", 'id': 2},...
我使用 objects.values()
做错了什么?如何使用 objects.values()
?
EDIT 当我使用 Question.objects.values()
时,它 return 是一个 dict
的数组,它没有属性 dict.pk
.但是当我做 Question.objects.all()
时,它 return 是一个 ModelResource
类型的数组,当我调用 ModelResource.pk
.
这些信息并没有多大帮助...哈哈。因为我确信解决方案就在 tastypie 或 django 库中。尽管如此,如果我能想出一种方法在 dict 上附加一个 pk 就可以解决我的问题。
values()
方法returns一个查询集,returns字典而不是模型实例。模型实例具有 pk
属性,但字典没有。 Tastypie 正在尝试访问字典中的 pk
,从而给出错误。您需要使用一种方法 returns 提供模型实例的查询集。
从 Django's documentation 使用 values()
:
when you know you’re only going to need values from a small number of the available fields and you won’t need the functionality of a model instance object.
在您的情况下,您确实需要模型实例对象的功能,pk
属性。
我建议使用 Question.objects.all()