Skip to content

Commit

Permalink
Revert "Do not include annotation fields when selects specific fields"
Browse files Browse the repository at this point in the history
This reverts commit ea8375e.
  • Loading branch information
devxoul committed Apr 29, 2022
1 parent ea8375e commit f91012f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
12 changes: 7 additions & 5 deletions modeltranslation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,10 @@ def raw_values(self, *fields):
return super(MultilingualQuerySet, self).values(*fields)

def _values(self, *original, **kwargs):
selects_all = not original
if not original:
# Emulate original queryset behaviour: get all fields that are not translation fields
original = self._get_original_fields()
if not kwargs.pop('prepare', False):
return super(MultilingualQuerySet, self)._values(*original, **kwargs)
new_fields, translation_fields = append_fallback(self.model, original)
annotation_keys = set(self.query.annotation_select.keys()) if selects_all else set()
annotation_keys = set(self.query.annotation_select.keys())
new_fields.update(annotation_keys)
clone = super(MultilingualQuerySet, self)._values(*list(new_fields), **kwargs)
clone.original_fields = tuple(original)
Expand All @@ -431,6 +427,9 @@ def _values(self, *original, **kwargs):
def values(self, *fields, **expressions):
if not self._rewrite:
return super(MultilingualQuerySet, self).values(*fields, **expressions)
if not fields:
# Emulate original queryset behaviour: get all fields that are not translation fields
fields = self._get_original_fields()
clone = self._values(*fields, prepare=True, **expressions)
clone._iterable_class = FallbackValuesIterable
return clone
Expand All @@ -446,6 +445,9 @@ def values_list(self, *fields, **kwargs):
raise TypeError(
"'flat' is not valid when values_list is " "called with more than one field."
)
if not fields:
# Emulate original queryset behaviour: get all fields that are not translation fields
fields = self._get_original_fields()
clone = self._values(*fields, prepare=True)
clone._iterable_class = (
FallbackFlatValuesListIterable if flat else FallbackValuesListIterable
Expand Down
11 changes: 0 additions & 11 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2919,17 +2919,6 @@ def test_values(self):
id1,
)

# custom annotation with fields specified
self.assertEqual(
list(manager.filter(id=id1).annotate(custom_id=F('id')).values_list('id'))[0],
(id1,),
)
self.assertIsNone(
list(manager.filter(id=id1).annotate(custom_id=F('id')).values('id'))[0].get(
'custom_id'
),
)

def test_values_list_annotation(self):
models.TestModel(title='foo').save()
models.TestModel(title='foo').save()
Expand Down

0 comments on commit f91012f

Please sign in to comment.