Please sign in to comment.
Refactored qs.add_q() and utils/tree.py
The sql/query.py add_q method did a lot of where/having tree hacking to get complex queries to work correctly. The logic was refactored so that it should be simpler to understand. The new logic should also produce leaner WHERE conditions. The changes cascade somewhat, as some other parts of Django (like add_filter() and WhereNode) expect boolean trees in certain format or they fail to work. So to fix the add_q() one must fix utils/tree.py, some things in add_filter(), WhereNode and so on. This commit also fixed add_filter to see negate clauses up the path. A query like .exclude(Q(reversefk__in=a_list)) didn't work similarly to .filter(~Q(reversefk__in=a_list)). The reason for this is that only the immediate parent negate clauses were seen by add_filter, and thus a tree like AND: (NOT AND: (AND: condition)) will not be handled correctly, as there is one intermediary AND node in the tree. The example tree is generated by .exclude(~Q(reversefk__in=a_list)). Still, aggregation lost connectors in OR cases, and F() objects and aggregates in same filter clause caused GROUP BY problems on some databases. Fixed #17600, fixed #13198, fixed #17025, fixed #17000, fixed #11293.
- Loading branch information...
Showing with 513 additions and 219 deletions.
- +3 −2 django/contrib/gis/db/models/sql/where.py
- +13 −0 django/db/models/aggregates.py
- +0 −1 django/db/models/constants.py
- +15 −3 django/db/models/expressions.py
- +11 −0 django/db/models/query_utils.py
- +4 −3 django/db/models/sql/compiler.py
- +12 −7 django/db/models/sql/expressions.py
- +137 −109 django/db/models/sql/query.py
- +3 −3 django/db/models/sql/subqueries.py
- +21 −13 django/db/models/sql/where.py
- +46 −66 django/utils/tree.py
- +85 −4 tests/aggregation_regress/tests.py
- +22 −0 tests/queries/models.py
- +141 −8 tests/queries/tests.py
Oops, something went wrong.