mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-02 18:44:43 +00:00
Merge pull request #321 from lukaslundgren/master
Handle annotation fields when using values_list
This commit is contained in:
commit
d0a3fe888a
2 changed files with 20 additions and 5 deletions
|
|
@ -445,14 +445,21 @@ class FallbackValuesQuerySet(models.query.ValuesQuerySet, MultilingualQuerySet):
|
|||
|
||||
class FallbackValuesListQuerySet(FallbackValuesQuerySet):
|
||||
def iterator(self):
|
||||
fields = self.original_fields
|
||||
if hasattr(self, 'aggregate_names'):
|
||||
# Django <1.8
|
||||
fields += tuple(self.aggregate_names)
|
||||
if hasattr(self, 'annotation_names'):
|
||||
# Django >=1.8
|
||||
fields += tuple(self.annotation_names)
|
||||
for row in super(FallbackValuesListQuerySet, self).iterator():
|
||||
if self.flat and len(self.original_fields) == 1:
|
||||
yield row[self.original_fields[0]]
|
||||
if self.flat and len(fields) == 1:
|
||||
yield row[fields[0]]
|
||||
else:
|
||||
yield tuple(row[f] for f in self.original_fields)
|
||||
yield tuple(row[f] for f in fields)
|
||||
|
||||
def _setup_query(self):
|
||||
self.original_fields = self._fields
|
||||
self.original_fields = tuple(self._fields)
|
||||
super(FallbackValuesListQuerySet, self)._setup_query()
|
||||
|
||||
def _clone(self, *args, **kwargs):
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ from django.core.files.base import ContentFile
|
|||
from django.core.files.storage import default_storage
|
||||
from django.core.management import call_command
|
||||
from django.db import IntegrityError
|
||||
from django.db.models import Q, F
|
||||
from django.db.models import Q, F, Count
|
||||
from django.test import TestCase, TransactionTestCase
|
||||
from django.test.utils import override_settings
|
||||
from django.utils import six
|
||||
|
|
@ -2551,6 +2551,14 @@ class TestManager(ModeltranslationTestBase):
|
|||
'description': None, 'description_en': None, 'description_de': None},
|
||||
])
|
||||
|
||||
def test_values_list_annotation(self):
|
||||
models.TestModel(title='foo').save()
|
||||
models.TestModel(title='foo').save()
|
||||
self.assertEqual(
|
||||
list(models.TestModel.objects.all().values_list('title').annotate(Count('id'))),
|
||||
[('foo', 2)]
|
||||
)
|
||||
|
||||
def test_custom_manager(self):
|
||||
"""Test if user-defined manager is still working"""
|
||||
n = models.CustomManagerTestModel(title='')
|
||||
|
|
|
|||
Loading…
Reference in a new issue