In django how to check whether any entry exists for a query
在django中,如何检查查询是否存在任何条目
sc=scorm.objects.filter(Header__id=qp.id)
This was how it was done in php
在php中就是这样做的
if(mysql_num_rows($resultn)) {
// True condition
}
else {
// False condition
}
2 个解决方案
#1
51
Use count()
:
使用count():
sc=scorm.objects.filter(Header__id=qp.id)
if sc.count() > 0:
...
The advantage over e.g. len()
is, that the QuerySet is not yet evaluated:
与示例len()相比的优点是,查询集还没有计算:
count()
performs aSELECT COUNT(*)
behind the scenes, so you should always usecount()
rather than loading all of the record into Python objects and callinglen()
on the result.count()在幕后执行SELECT count(*),因此应该始终使用count(),而不是将所有记录加载到Python对象中并在结果上调用len()。
Having this in mind, When QuerySets are evaluated can be worth reading.
考虑到这一点,当对queryset进行评估时,它是值得一读的。
If you use get()
, e.g. scorm.objects.get(pk=someid)
, and the object does not exists, an ObjectDoesNotExist
exception is raised:
如果您使用get(),例如scorm.objects.get(pk=someid),而对象不存在,则会引发objectnotexist异常:
from django.core.exceptions import ObjectDoesNotExist
try:
sc = scorm.objects.get(pk=someid)
except ObjectDoesNotExist:
print ...
Update: it's also possible to use exists()
:
更新:也可以使用exist ():
if scorm.objects.filter(Header__id=qp.id).exists():
....
Returns
True
if the QuerySet contains any results, andFalse
if not. This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query.如果QuerySet包含任何结果,返回True;如果不包含,返回False。这试图以最简单、最快的方式执行查询,但它执行的查询与普通的QuerySet查询几乎相同。
#2
118
As of Django 1.2, you can use exists()
:
对于Django 1.2,可以使用exist ():
https://docs.djangoproject.com/en/dev/ref/models/querysets/#exists
https://docs.djangoproject.com/en/dev/ref/models/querysets/存在
if some_queryset.filter(pk=entity_id).exists():
print("Entry contained in queryset")
#1
51
Use count()
:
使用count():
sc=scorm.objects.filter(Header__id=qp.id)
if sc.count() > 0:
...
The advantage over e.g. len()
is, that the QuerySet is not yet evaluated:
与示例len()相比的优点是,查询集还没有计算:
count()
performs aSELECT COUNT(*)
behind the scenes, so you should always usecount()
rather than loading all of the record into Python objects and callinglen()
on the result.count()在幕后执行SELECT count(*),因此应该始终使用count(),而不是将所有记录加载到Python对象中并在结果上调用len()。
Having this in mind, When QuerySets are evaluated can be worth reading.
考虑到这一点,当对queryset进行评估时,它是值得一读的。
If you use get()
, e.g. scorm.objects.get(pk=someid)
, and the object does not exists, an ObjectDoesNotExist
exception is raised:
如果您使用get(),例如scorm.objects.get(pk=someid),而对象不存在,则会引发objectnotexist异常:
from django.core.exceptions import ObjectDoesNotExist
try:
sc = scorm.objects.get(pk=someid)
except ObjectDoesNotExist:
print ...
Update: it's also possible to use exists()
:
更新:也可以使用exist ():
if scorm.objects.filter(Header__id=qp.id).exists():
....
Returns
True
if the QuerySet contains any results, andFalse
if not. This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query.如果QuerySet包含任何结果,返回True;如果不包含,返回False。这试图以最简单、最快的方式执行查询,但它执行的查询与普通的QuerySet查询几乎相同。
#2
118
As of Django 1.2, you can use exists()
:
对于Django 1.2,可以使用exist ():
https://docs.djangoproject.com/en/dev/ref/models/querysets/#exists
https://docs.djangoproject.com/en/dev/ref/models/querysets/存在
if some_queryset.filter(pk=entity_id).exists():
print("Entry contained in queryset")