Ticket #17712: 17712.diff

File 17712.diff, 2.6 KB (added by akaariai, 4 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 41c24c7..9cadbf0 100644
    a b class ValuesQuerySet(QuerySet): 
    10271027
    10281028    def _as_sql(self, connection):
    10291029        """
    1030         For ValueQuerySet (and subclasses like ValuesListQuerySet), they can
     1030        For ValuesQuerySet (and subclasses like ValuesListQuerySet), they can
    10311031        only be used as nested queries if they're already set up to select only
    10321032        a single field (in which case, that is the field column that is
    10331033        returned). This differs from QuerySet.as_sql(), where the column to
    class EmptyQuerySet(QuerySet): 
    12331233            kwargs[arg.default_alias] = arg
    12341234        return dict([(key, None) for key in kwargs])
    12351235
     1236    def values(self, *args):
     1237        """
     1238        Technically, this should return an EmptyValuesQuerySet (a subclass
     1239        of both this and ValuesQuerySet classes). However, we return just
     1240        EmptyQuerySet.
     1241        """
     1242        return self
     1243   
     1244    def values_list(self, *args, **kwargs):
     1245        """
     1246        Similarly to above, this should return an EmptyValuesListQuerySet.
     1247        However, this returns just EmptyQuerySet.
     1248        """
     1249        return self
     1250
    12361251    # EmptyQuerySet is always an empty result in where-clauses (and similar
    12371252    # situations).
    12381253    value_annotation = False
  • tests/regressiontests/queries/tests.py

    diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
    index ded3e8f..82e083d 100644
    a b class CloneTests(TestCase): 
    16121612
    16131613
    16141614class EmptyQuerySetTests(TestCase):
     1615    def setUp(self):
     1616        # Addition for ticket #17712: have actually some data in the numbers
     1617        # table, so that the below tests could return something.
     1618        Number.objects.create(num=1)
     1619
    16151620    def test_emptyqueryset_values(self):
    16161621        # #14366 -- Calling .values() on an EmptyQuerySet and then cloning that
    16171622        # should not cause an error"
    16181623        self.assertQuerysetEqual(
    16191624            Number.objects.none().values('num').order_by('num'), []
    16201625        )
     1626   
     1627    def test_emptyqueryset_values_empty(self):
     1628        # #17712 none().values() / .none().values_list() should not return
     1629        # anything
     1630        self.assertQuerysetEqual(
     1631            Number.objects.none().values('num'), []
     1632        )
     1633        self.assertQuerysetEqual(
     1634            Number.objects.none().values_list('num'), []
     1635        )
    16211636
    16221637    def test_values_subquery(self):
    16231638        self.assertQuerysetEqual(
Back to Top