﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24705	Exception when negating Q object in annotate	karyon	nobody	"any of the following
{{{
UserProfile.objects.annotate(is_member=ExpressionWrapper(~Q(groups__name=""Group name""), output_field=BooleanField()))

UserProfile.objects.annotate(is_member=Case(When(~models.Q(groups__name='group name'), then=Value(False)), default=Value(True), output_field=models.BooleanField()))

UserProfile.objects.annotate(is_member=Case(When(~Q(groups__name=""Group name""), then=Value(False)), default=Value(True), output_field=BooleanField()))
}}}

gives the following exception:


{{{
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/manager.py"", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/query.py"", line 794, in annotate
    obj.query.add_annotation(annotation, alias, is_summary=False)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py"", line 977, in add_annotation
    summarize=is_summary)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/expressions.py"", line 779, in resolve_expression
    c.cases[pos] = case.resolve_expression(query, allow_joins, reuse, summarize, for_save)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/expressions.py"", line 713, in resolve_expression
    c.condition = c.condition.resolve_expression(query, allow_joins, reuse, summarize, False)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/query_utils.py"", line 88, in resolve_expression
    clause, _ = query._add_q(self, reuse, allow_joins=allow_joins)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py"", line 1328, in _add_q
    current_negated=current_negated, connector=connector, allow_joins=allow_joins)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py"", line 1177, in build_filter
    can_reuse, e.names_with_path)
  File ""/usr/local/lib/python3.4/dist-packages/django/db/models/sql/query.py"", line 1570, in split_exclude
    if alias in can_reuse:
TypeError: argument of type 'NoneType' is not iterable
}}}


removing the ~ ""fixes"" this.

see https://github.com/fsr-itse/EvaP for full code (a custom UserProfile is in evaluation.models), but it looks like this also happens with the default User model.
"	Bug	closed	Database layer (models, ORM)	1.8	Normal	fixed		karyon Markus Holtermann	Accepted	1	0	0	1	0	0
