可能在查询后过滤查询集?詹戈 [英] possible to filter the queryset after querying? django
问题描述
很抱歉,这个问题听起来很奇怪.我只是想知道当我已经有一个查询集时是否可以制作新的查询集.
Sorry if the question sounds weird. I am just wondering if there is possible to make new queryset when I already have a queryset.
例如此处...
everyone = User.objects.filter(is_active=True) # this would of course return all users that's active
not_deleted = User.objects.filter(is_active=True, is_deleted=False) # return user that's active and not deleted
is_deleted = User.objects.filter(is_active=True, is_deleted=True) # return user that's active and is already deleted
我的问题是什么...对于not_deleted
和is_deleted
它们都处于活动状态,它与everyone
相同,是否有可能使用everyone
,然后以某种方式滤除is_deleted=True
还是is_deleted=False
?因此,我相信如果可以的话,查询会更快更好.
What my question is...for not_deleted
and is_deleted
they both have active is true with is the same as everyone
is there a possible way to use everyone
and then somehow filter out is_deleted=True
or is_deleted=False
? So then I believe the querying would be faster and better if this is possible right?
然后将所有三个变量everyone
,not_deleted
和is_deleted
用于其他用途.
All three variables everyone
, not_deleted
and is_deleted
will then be used for something else.
希望我的问题已经清楚了.
Hopefully I made my question quiet clear.
谢谢.
推荐答案
是的,您可以重用现有的查询集.
Yes, you can reuse existing querysets.
everyone = User.objects.filter(is_active=True)
active_not_deleted = everyone.filter(is_deleted=False)
active_is_deleted = everyone.filter(is_deleted=True)
这实际上并没有使任何事情变得更快,实际上,因为Django QuerySets是惰性计算的,所以该代码块甚至不会对数据库执行查询.我的意思是,除非您真正需要这些值,否则它不会将查询发送到数据库.这是一个与数据库对话的示例.
This is not really making anything faster though, in fact, this code block won't even execute a query against the database because Django QuerySets are lazily evaluated. What I means is that it won't send the query to the database until you actually need the values. Here's an example that will talk to the database.
everyone = User.objects.filter(is_active=True) # Building SQL...
active_not_deleted = everyone.filter(is_deleted=False) # Building SQL...
active_is_deleted = everyone.filter(is_deleted=True) # Building SQL...
# Example of the whole queryset being evaluated
for user in everyone:
# This will execute the query against the database to return the list of users
# i.e. "select * from user where is_active is True;"
print(user)
# Example of using iterator to evaluate one object at a time from the queryset.
for user in active_not_deleted.iterator():
# This will execute the query for each result, so it doesn't
# load everything at once and it doesn't cache the results.
# "select * from user where is_active is True and is_deleted is False limit 1 offset 0;"
# The offset is incremented on each loop and another query is sent to retrieve the next user in the list.
print(user)
推荐阅读:
- https://docs.djangoproject.com /en/1.11/topics/db/queries/#querysets-are-lazy
- https://docs.djangoproject.com/en/1.11 /ref/models/querysets/#iterator
- https://docs.djangoproject.com /en/1.11/topics/db/queries/#caching-and-querysets
- https://docs.djangoproject.com/en/1.11/topics/db/queries/#querysets-are-lazy
- https://docs.djangoproject.com/en/1.11/ref/models/querysets/#iterator
- https://docs.djangoproject.com/en/1.11/topics/db/queries/#caching-and-querysets
作为此答案的补充,您可以进行单个查询,然后根据需要在Python中进行过滤.请注意,您不能对列表进行后续过滤,因为它们不是QuerySet.
As an addition to this answer, you could make a single query and then filter in Python if you really wanted. Mind you, you could not do subsequent filtering on the lists because they are not QuerySets.
everyone = User.objects.filter(is_active=True)
active_not_deleted = list(filter(lambda user: user.is_deleted is False), list(everyone))
active_is_deleted = list(filter(lambda user: user.is_deleted is True), list(everyone))
在最后一个示例中,everyone
是一个查询集,而active_not_deleted
和active_is_deleted
是User对象的Python列表. everyone
查询集仅在第一次list(everyone)
调用中被评估一次,然后将结果缓存.
In this last example, everyone
is a queryset, and active_not_deleted
and active_is_deleted
are Python lists of User objects. The everyone
queryset will only be evaluated once in the first list(everyone)
call, and then the results are cached.
这篇关于可能在查询后过滤查询集?詹戈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!