From b87e2f46c80d15abcda0e181ab894fb938ceb12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= Date: Sun, 21 Oct 2012 00:11:16 +0200 Subject: [PATCH] Fixed #19151 -- Added missing methods to EmptyQuerySet. Added values() and values_list() methods to EmptyQuerySet. --- django/db/models/query.py | 12 ++++++++++++ tests/regressiontests/queries/tests.py | 7 +++++++ 2 files changed, 19 insertions(+) diff --git a/django/db/models/query.py b/django/db/models/query.py index 0210a7914dc..412cad026f6 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1271,6 +1271,18 @@ class EmptyQuerySet(QuerySet): kwargs[arg.default_alias] = arg return dict([(key, None) for key in kwargs]) + def values(self, *fields): + """ + Always returns EmptyQuerySet. + """ + return self + + def values_list(self, *fields, **kwargs): + """ + Always returns EmptyQuerySet. + """ + return self + # EmptyQuerySet is always an empty result in where-clauses (and similar # situations). value_annotation = False diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py index 71ac107486c..c2a4ad1caf4 100644 --- a/tests/regressiontests/queries/tests.py +++ b/tests/regressiontests/queries/tests.py @@ -1698,6 +1698,13 @@ class EmptyQuerySetTests(TestCase): [] ) + def test_ticket_19151(self): + # #19151 -- Calling .values() or .values_list() on an EmptyQuerySet + # should return EmptyQuerySet and not cause an error. + q = EmptyQuerySet() + self.assertQuerysetEqual(q.values(), []) + self.assertQuerysetEqual(q.values_list(), []) + class ValuesQuerysetTests(BaseQuerysetTest): def test_flat_values_lits(self):