                      allow_explicit_fk=True)
              if can_reuse is not None:
                  can_reuse.update(join_list)
          except MultiJoin as e:
              self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
                                 can_reuse, e.names_with_path)
              return
  
          if (lookup_type == 'isnull' and value is True and not negate and
                  len(join_list) > 1):
              # If the comparison is against NULL, we may need to use some left
              # outer joins when creating the join chain. This is only done when
 
</s> remove         if node in self.children and conn_type == self.connector:
            return
        if len(self.children) < 2:
            self.connector = conn_type
        if self.connector == conn_type:
            if isinstance(node, Node) and (node.connector == conn_type or
                    len(node) == 1):
                self.children.extend(node.children)
            else:
                self.children.append(node)
        else:
            obj = self._new_instance(self.children, self.connector,
                    self.negated)
            self.connector = conn_type
            self.children = [obj, node]
 </s> add         return data
            return self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
                                      can_reuse, e.names_with_path)
                      allow_explicit_fk=True)
              if can_reuse is not None:
                  can_reuse.update(join_list)
          except MultiJoin as e:
            return self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
                                      can_reuse, e.names_with_path)
            return self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
                                      can_reuse, e.names_with_path)
            return self.split_exclude(filter_expr, LOOKUP_SEP.join(parts[:e.level]),
                                      can_reuse, e.names_with_path)
  
          if (lookup_type == 'isnull' and value is True and not negate and
                  len(join_list) > 1):
              # If the comparison is against NULL, we may need to use some left
              # outer joins when creating the join chain. This is only done when
keep keep keep keep replace replace replace keep keep keep keep keep
 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. 
django/db/models/sql/query.py