本文整理汇总了Python中django.db.models.query.EmptyQuerySet方法的典型用法代码示例。如果您正苦于以下问题:Python query.EmptyQuerySet方法的具体用法?Python query.EmptyQuerySet怎么用?Python query.EmptyQuerySet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.query
的用法示例。
在下文中一共展示了query.EmptyQuerySet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_and
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_and(self):
"""ANDing with a QuerySet applies the and to each QuerySet and removes ones of differing types."""
# ANDing with a different type of QuerySet ends up with an EmptyQuerySet.
with self.assertNumQueries(0):
combined = self.all & BlogPost.objects.all()
self.assertIsInstance(combined, EmptyQuerySet)
# ANDing with a QuerySet of a type in the QuerySetSequence applies the
# AND to that QuerySet.
with self.assertNumQueries(0):
combined = self.all & Book.objects.filter(pages__lt=15)
self.assertIsInstance(combined, QuerySetSequence)
self.assertEqual(len(combined._querysets), 1)
with self.assertNumQueries(1):
data = [it.title for it in combined.iterator()]
self.assertEqual(data, ['Fiction'])
示例2: __and__
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def __and__(self, other):
# If the other QuerySet is an EmptyQuerySet, this is a no-op.
if isinstance(other, EmptyQuerySet):
return other
combined = self._clone()
querysets = []
for qs in combined._querysets:
# Only QuerySets of the same type can have any overlap.
if issubclass(qs.model, other.model):
querysets.append(qs & other)
# If none are left, we're left with an EmptyQuerySet.
if not querysets:
return other.none()
combined._set_querysets(querysets)
return combined
示例3: filter
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def filter(self, *args, **kwargs):
"""
Returns a new QuerySetSequence or instance with the args ANDed to the
existing set.
QuerySetSequence is simplified thus result actually can be one of:
QuerySetSequence, QuerySet, EmptyQuerySet.
"""
return self._filter_or_exclude(False, *args, **kwargs)
示例4: exclude
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def exclude(self, *args, **kwargs):
"""
Returns a new QuerySetSequence instance with NOT (args) ANDed to the
existing set.
QuerySetSequence is simplified thus result actually can be one of:
QuerySetSequence, QuerySet, EmptyQuerySet.
"""
return self._filter_or_exclude(True, *args, **kwargs)
示例5: _simplify
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def _simplify(self, qss=None):
'''
Returns QuerySetSequence, QuerySet or EmptyQuerySet depending on the
contents of items, i.e. at least two non empty QuerySets, exactly one
non empty QuerySet and all empty QuerySets respectively.
Does not modify original QuerySetSequence.
'''
not_empty_qss = filter(None, qss if qss else self.iables)
if not len(not_empty_qss):
return EmptyQuerySet()
if len(not_empty_qss) == 1:
return not_empty_qss[0]
return QuerySetSequence(*not_empty_qss)
示例6: test_and_identity
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_and_identity(self):
"""ANDing with an EmptyQuerySet returns an EmptyQuerySet."""
with self.assertNumQueries(0):
combined = self.all & BlogPost.objects.none()
self.assertIsInstance(combined, EmptyQuerySet)
示例7: test_empty_and
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_empty_and(self):
"""An empty QuerySetSequence can be ANDed with a QuerySet, but returns an EmptyQuerySet."""
combined = self.empty & BlogPost.objects.all()
self.assertIsInstance(combined, EmptyQuerySet)
self.assertEqual(list(combined), [])
示例8: test_empty_or
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_empty_or(self):
"""An empty QuerySetSequence can be ORed with a QuerySet, but returns an EmptyQuerySet."""
combined = self.empty | BlogPost.objects.all()
self.assertIsInstance(combined, QuerySetSequence)
self.assertEqual(len(combined._querysets), 1)
with self.assertNumQueries(1):
data = [it.title for it in combined.iterator()]
self.assertEqual(data, ['Post'])
示例9: test_none
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_none(self):
"""
Ensure an instance of EmptyQuerySet is returned and has no results (and
doesn't perform queries).
"""
with self.assertNumQueries(0):
qss = self.all.none()
data = list(qss)
# This returns an EmptyQuerySet.
self.assertIsInstance(qss, EmptyQuerySet)
# Should have no data.
self.assertEqual(data, [])
示例10: __or__
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def __or__(self, other):
# If the other QuerySet is an EmptyQuerySet, this is a no-op.
if isinstance(other, EmptyQuerySet):
return self
combined = self._clone()
# If the other instance is a QuerySetSequence, combine the QuerySets.
if isinstance(other, QuerySetSequence):
combined._set_querysets(self._querysets + other._querysets)
elif isinstance(other, QuerySet):
combined._set_querysets(self._querysets + [other])
return combined
示例11: none
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def none(self):
# This is a bit odd, but use the first QuerySet to properly return an
# that is an instance of EmptyQuerySet.
return self._querysets[0].none()
示例12: get_empty_query_set
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def get_empty_query_set(self):
return EmptyQuerySet(self.model, using=self._db)
示例13: test_model_multiple_choice_required_false
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_model_multiple_choice_required_false(self):
f = forms.ModelMultipleChoiceField(Category.objects.all(), required=False)
self.assertIsInstance(f.clean([]), EmptyQuerySet)
self.assertIsInstance(f.clean(()), EmptyQuerySet)
with self.assertRaises(ValidationError):
f.clean(['0'])
with self.assertRaises(ValidationError):
f.clean([str(self.c3.id), '0'])
with self.assertRaises(ValidationError):
f.clean([str(self.c1.id), '0'])
# queryset can be changed after the field is created.
f.queryset = Category.objects.exclude(name='Third')
self.assertEqual(list(f.choices), [
(self.c1.pk, 'Entertainment'),
(self.c2.pk, "It's a test")])
self.assertQuerysetEqual(f.clean([self.c2.id]), ["It's a test"])
with self.assertRaises(ValidationError):
f.clean([self.c3.id])
with self.assertRaises(ValidationError):
f.clean([str(self.c2.id), str(self.c3.id)])
f.queryset = Category.objects.all()
f.label_from_instance = lambda obj: "multicategory " + str(obj)
self.assertEqual(list(f.choices), [
(self.c1.pk, 'multicategory Entertainment'),
(self.c2.pk, "multicategory It's a test"),
(self.c3.pk, 'multicategory Third')])
示例14: test_emptyqs
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_emptyqs(self):
msg = "EmptyQuerySet can't be instantiated"
with self.assertRaisesMessage(TypeError, msg):
EmptyQuerySet()
self.assertIsInstance(Article.objects.none(), EmptyQuerySet)
self.assertNotIsInstance('', EmptyQuerySet)
示例15: test_emptyqs_values
# 需要导入模块: from django.db.models import query [as 别名]
# 或者: from django.db.models.query import EmptyQuerySet [as 别名]
def test_emptyqs_values(self):
# test for #15959
Article.objects.create(headline='foo', pub_date=datetime.now())
with self.assertNumQueries(0):
qs = Article.objects.none().values_list('pk')
self.assertIsInstance(qs, EmptyQuerySet)
self.assertEqual(len(qs), 0)