Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tree queryset .values() correctly #57

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading