Ticket #10847: values-extra.2.diff

File values-extra.2.diff, 2.2 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 9323e9b..6716dc0 100644
    a b class ValuesQuerySet(QuerySet):  
    696696        # QuerySet.clone() will also set up the _fields attribute with the
    697697        # names of the model fields to select.
    698698
     699    def extra(self, *args, **kwargs):
     700        q = super(ValuesQuerySet, self).extra(*args, **kwargs)
     701        if q.extra_names is not None:
     702            q.query.trim_extra_select(q.extra_names)
     703        return q
     704
    699705    def iterator(self):
    700706        # Purge any extra columns that haven't been explicitly asked for
    701707        if self.extra_names is not None:
    class ValuesQuerySet(QuerySet):  
    747753        if self.aggregate_names is not None:
    748754            self.query.set_aggregate_mask(self.aggregate_names)
    749755
     756        if self.extra_names is not None:
     757            self.query.trim_extra_select(self.extra_names)
     758
    750759    def _clone(self, klass=None, setup=False, **kwargs):
    751760        """
    752761        Cloning a ValuesQuerySet preserves the current fields.
  • tests/regressiontests/extra_regress/models.py

    diff --git a/tests/regressiontests/extra_regress/models.py b/tests/regressiontests/extra_regress/models.py
    index fd34982..121076e 100644
    a b class TestObject(models.Model):  
    3535    second = models.CharField(max_length=20)
    3636    third = models.CharField(max_length=20)
    3737
     38    def __unicode__(self):
     39        return "%s-%s-%s" % (self.first, self.second, self.third)
     40
    3841__test__ = {"API_TESTS": """
    3942# Regression tests for #7314 and #7372
    4043
    True  
    189192>>> TestObject.objects.extra(select=SortedDict((('foo','first'),('bar','second'),('whiz','third')))).values_list('whiz', 'first', 'bar', 'id')
    190193[(u'third', u'first', u'second', 1)]
    191194
    192 """}
     195>>> list(TestObject.objects.filter(pk__in=TestObject.objects.extra(select={'extra': 1}).values('pk'))) == list(TestObject.objects.all())
     196True
     197
     198>>> TestObject.objects.values('pk').extra(select={'extra': 1})
     199[{'pk': 1}]
    193200
     201>>> TestObject.objects.filter(pk__in=TestObject.objects.values('pk').extra(select={'extra': 1}))
     202[<TestObject: first-second-third>]
    194203
     204"""}
Back to Top