Skip to content

Commit

Permalink
fix: Preserve annotate() fields in queryset (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
devxoul authored Apr 29, 2022
1 parent 86c3a52 commit 6f2688f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion modeltranslation/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,12 @@ def _values(self, *original, **kwargs):
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())
new_fields.update(annotation_keys)
clone = super(MultilingualQuerySet, self)._values(*list(new_fields), **kwargs)
clone.original_fields = tuple(original)
clone.translation_fields = translation_fields
clone.fields_to_del = new_fields - set(original)
clone.fields_to_del = new_fields - annotation_keys - set(original)
return clone

# This method was not present in django-linguo
Expand Down
10 changes: 10 additions & 0 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2909,6 +2909,16 @@ def test_values(self):
list(manager.values_list('title', flat=True).annotate(Count('title'))), ['en']
)

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

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

0 comments on commit 6f2688f

Please # to comment.