I had a sniff around SO and couldn't find this, but I am sure it is here somewhere. Apologies for this potential double post!
我四处嗅了一下,没找到这个,但我肯定它就在这里。为这个潜在的双关语道歉!
If I have this code:
如果我有这个代码:
return Story.objects.filter(user=request.user.id).order_by('-create_date')
and say Story has, um, a "description" field, and I just want that description field, no need for the db to send anything else back with my result, how do I limit the query to just that?
说Story有一个"description"字段,我只是想要那个description字段,不需要db发送任何其他东西给我的结果,我如何限制查询?
That is, how do I generate this SQL:
也就是说,如何生成这个SQL:
select description from story where user_id = x order by create_date desc
(where x is the request.user.id value, of course)
(其中x是request。user。当然,id值)
3 个解决方案
#1
38
Use values()
or values_list()
.
使用值()或values_list()。
If you use values()
, You'll end up with a list of dictionaries (technically a ValuesQuerySet
)
如果您使用values(),您将得到一个字典列表(技术上是ValuesQuerySet)
instance = MyModel.objects.values('description')[0]
description = instance['description']
If you use values_list()
, you'll end up with a list of tuples
如果您使用values_list(),您将得到一个元组列表
instance = MyModel.objects.values_list('description')[0]
description = instance[0]
Or if you're just getting one value like in this case, you can use the flat=True
kwarg with values_list
to get a simple list of values
或者,如果您只获得一个值,比如在本例中,您可以使用flat=True kwarg with values_list来获得一个简单的值列表
description = MyModel.objects.values_list('description', flat=True)[0]
#3
1
As the docs says if the your query set is going to yield only one object, it's recommended that you should use get() instead of filter. From the name that you've given, I would assume that user_id is distinct for every user. EDIT1 : I just took a notice of orderby clause. So I would suggest to use this code if the user_id is distinct or with pk.
正如文档中所说,如果您的查询集只生成一个对象,那么建议您使用get()而不是filter。根据您给出的名称,我假设user_id对于每个用户都是不同的。我刚注意到欧德比条款。如果user_id是不同的,或者是pk,我建议使用这段代码。
description = story.objects.filter('description').get(user_id=x)
#1
38
Use values()
or values_list()
.
使用值()或values_list()。
If you use values()
, You'll end up with a list of dictionaries (technically a ValuesQuerySet
)
如果您使用values(),您将得到一个字典列表(技术上是ValuesQuerySet)
instance = MyModel.objects.values('description')[0]
description = instance['description']
If you use values_list()
, you'll end up with a list of tuples
如果您使用values_list(),您将得到一个元组列表
instance = MyModel.objects.values_list('description')[0]
description = instance[0]
Or if you're just getting one value like in this case, you can use the flat=True
kwarg with values_list
to get a simple list of values
或者,如果您只获得一个值,比如在本例中,您可以使用flat=True kwarg with values_list来获得一个简单的值列表
description = MyModel.objects.values_list('description', flat=True)[0]
#2
#3
1
As the docs says if the your query set is going to yield only one object, it's recommended that you should use get() instead of filter. From the name that you've given, I would assume that user_id is distinct for every user. EDIT1 : I just took a notice of orderby clause. So I would suggest to use this code if the user_id is distinct or with pk.
正如文档中所说,如果您的查询集只生成一个对象,那么建议您使用get()而不是filter。根据您给出的名称,我假设user_id对于每个用户都是不同的。我刚注意到欧德比条款。如果user_id是不同的,或者是pk,我建议使用这段代码。
description = story.objects.filter('description').get(user_id=x)