Just came across this in the django docs
刚刚在django文档中遇到过这个问题
Calling none() will create a queryset that never returns any objects and no query will be executed when accessing the results. A qs.none() queryset is an instance of EmptyQuerySet.
调用none()将创建一个永远不会返回任何对象的查询集,并且在访问结果时不会执行任何查询。 qs.none()queryset是EmptyQuerySet的一个实例。
I build a lot of CRUD apps (surprise) and I can't think of a situation where I would need to use none()
.
我构建了很多CRUD应用程序(惊喜),我想不出我需要使用none()的情况。
Why would one want to return an EmptyQuerySet?
为什么要返回EmptyQuerySet?
3 个解决方案
#1
12
Usually in instances where you need to provide a QuerySet
, but there isn't one to provide - such as calling a method or to give to a template.
通常在需要提供QuerySet但不提供QuerySet的情况下 - 例如调用方法或提供模板。
The advantage is if you know there is going to be no result (or don't want a result) and you still need one, none()
will not hit the database.
优点是如果你知道没有结果(或者不想要结果)并且你仍然需要一个,none()将不会命中数据库。
For a non-realistic example, say you have an API where you can query your permissions. If the account hasn't been confirmed, since you already have the Account
object and you can see that account.is_activated
is False
, you could skip checking the database for permissions by just using Permission.objects.none()
对于一个不现实的示例,假设您有一个API,您可以在其中查询您的权限。如果帐户尚未确认,因为您已经拥有Account对象,并且您可以看到account.is_activated为False,您可以通过仅使用Permission.objects.none()跳过检查数据库的权限
#2
5
In cases where you want to append to querysets but want an empty one to begin with Similar to conditions where we instantiate an empty list to begin with but gradually keep appending meaningful values to it example..
如果您想要附加到查询集但希望开始使用空的类似于我们实例化一个空列表开始的条件,但逐渐向它添加有意义的值示例..
def get_me_queryset(conditionA, conditionB, conditionC):
queryset = Model.objects.none()
if conditionA:
queryset |= some_complex_computation(conditionA)
elif conditionB:
queryset |= some_complex_computation(conditionB)
if conditionC:
queryset |= some_simple_computation(conditionC)
return queryset
get_me_queryset should almost always return instance of django.db.models.query.QuerySet (because good programming) and not None or [], or else it will introduce headaches later.. . This ways even if none of the conditions come True, your code will still remain intact. No more type checking
get_me_queryset应该几乎总是返回django.db.models.query.QuerySet的实例(因为编程好)而不是None或[],否则以后会引入头痛......这种方式即使没有任何条件成立,您的代码仍将保持不变。没有更多类型检查
#3
0
another use of queryset.none is when you don't know if there will be objects but do not want to raise an error.
queryset.none的另一个用途是当你不知道是否会有对象但又不想引发错误时。
example:
例:
class DummyMixin(object):
def get_context_data(self,**kwargs):
""" Return all the pks of objects into the context """
context = super(DummyMixin, self).get_context_data(**kwargs)
objects_pks = context.get(
"object_list",
Mymodel.objects.none()
).values_list("pk", flat=True)
context["objects_pks"] = objects_pks
#1
12
Usually in instances where you need to provide a QuerySet
, but there isn't one to provide - such as calling a method or to give to a template.
通常在需要提供QuerySet但不提供QuerySet的情况下 - 例如调用方法或提供模板。
The advantage is if you know there is going to be no result (or don't want a result) and you still need one, none()
will not hit the database.
优点是如果你知道没有结果(或者不想要结果)并且你仍然需要一个,none()将不会命中数据库。
For a non-realistic example, say you have an API where you can query your permissions. If the account hasn't been confirmed, since you already have the Account
object and you can see that account.is_activated
is False
, you could skip checking the database for permissions by just using Permission.objects.none()
对于一个不现实的示例,假设您有一个API,您可以在其中查询您的权限。如果帐户尚未确认,因为您已经拥有Account对象,并且您可以看到account.is_activated为False,您可以通过仅使用Permission.objects.none()跳过检查数据库的权限
#2
5
In cases where you want to append to querysets but want an empty one to begin with Similar to conditions where we instantiate an empty list to begin with but gradually keep appending meaningful values to it example..
如果您想要附加到查询集但希望开始使用空的类似于我们实例化一个空列表开始的条件,但逐渐向它添加有意义的值示例..
def get_me_queryset(conditionA, conditionB, conditionC):
queryset = Model.objects.none()
if conditionA:
queryset |= some_complex_computation(conditionA)
elif conditionB:
queryset |= some_complex_computation(conditionB)
if conditionC:
queryset |= some_simple_computation(conditionC)
return queryset
get_me_queryset should almost always return instance of django.db.models.query.QuerySet (because good programming) and not None or [], or else it will introduce headaches later.. . This ways even if none of the conditions come True, your code will still remain intact. No more type checking
get_me_queryset应该几乎总是返回django.db.models.query.QuerySet的实例(因为编程好)而不是None或[],否则以后会引入头痛......这种方式即使没有任何条件成立,您的代码仍将保持不变。没有更多类型检查
#3
0
another use of queryset.none is when you don't know if there will be objects but do not want to raise an error.
queryset.none的另一个用途是当你不知道是否会有对象但又不想引发错误时。
example:
例:
class DummyMixin(object):
def get_context_data(self,**kwargs):
""" Return all the pks of objects into the context """
context = super(DummyMixin, self).get_context_data(**kwargs)
objects_pks = context.get(
"object_list",
Mymodel.objects.none()
).values_list("pk", flat=True)
context["objects_pks"] = objects_pks