diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index c8857a01fe..3e8bfed087 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -51,12 +51,12 @@ class WhereNode(tree.Node): format = '(%s)' elif isinstance(child, tree.Node): sql, params = self.as_sql(child, qn) - if len(child.children) == 1: + if child.negated: + format = 'NOT (%s)' + elif len(child.children) == 1: format = '%s' else: format = '(%s)' - if child.negated: - format = 'NOT %s' % format else: sql, params = self.make_atom(child, qn) format = '%s'