Skip to content

Commit

Permalink
Handle tree queryset .values() correctly (#57)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Kestenholz <[email protected]>
  • Loading branch information
glennmatthews and matthiask authored Nov 29, 2023
1 parent 4ee1a96 commit 80feec8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
20 changes: 20 additions & 0 deletions tests/testapp/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ def test_update_aggregate(self):
)

def test_values(self):
tree = self.create_tree()
self.assertEqual(
list(Model.objects.ancestors(tree.child2_1).values()),
[
{
"custom_id": tree.root.pk,
"name": "root",
"order": 0,
"parent_id": None,
},
{
"custom_id": tree.child2.pk,
"name": "2",
"order": 1,
"parent_id": tree.root.pk,
},
],
)

def test_values_list(self):
tree = self.create_tree()
self.assertEqual(
list(
Expand Down
12 changes: 7 additions & 5 deletions tree_queries/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,13 @@ def as_sql(self, *args, **kwargs):
# I am not confident that this is the perfect way to approach this
# problem but I just gotta stop worrying and trust the testsuite.
skip_tree_fields = (
self.query.distinct and self.query.subquery
) or any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
(self.query.distinct and self.query.subquery)
or self.query.values_select
or any( # pragma: no branch
# OK if generator is not consumed completely
annotation.is_summary
for alias, annotation in self.query.annotations.items()
)
)
opts = _find_tree_model(self.query.model)._meta

Expand Down

0 comments on commit 80feec8

Please sign in to comment.