The problem: When using annotate on a queryset from objects.all() and then using an exclude via a related table, accessing the queryset causes an AttributeError.
Please note that excluding first, and then calling annotate works. This is however not possible in my case, because I want the annotate field to be included in the queryset that the objects manager returns. I've attached code from a shell to illustrate the problem: Python 2.4.3 (#1, Sep 3 2009, 15:37:12) IPython 0.8.4 -- An enhanced Interactive Python. django (1, 1, 0, 'final', 0) In [1]: from core.models import * In [2]: from django.db.models import get_model, Q, Count, Sum, Min In [3]: print InvoiceLine.objects.all().exclude(invoice__flag__in= ('f')).annotate(test=Sum('invoice__id')).query < Works > In [4]: print InvoiceLine.objects.all().annotate(test=Sum ('invoice__id')).exclude(invoice__flag__in=('f')).query --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /usr/lib/python2.4/site-packages/django/db/models/sql/query.pyc in __str__(self) 111 done by the database interface at execution time. 112 """ --> 113 sql, params = self.as_sql() 114 return sql % params 115 /usr/lib/python2.4/site-packages/django/db/models/sql/query.pyc in as_sql(self, with_limits, with_col_aliases) 402 403 qn = self.quote_name_unless_alias --> 404 where, w_params = self.where.as_sql(qn=qn) 405 having, h_params = self.having.as_sql(qn=qn) 406 params = [] /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in as_sql(self, qn) 98 try: 99 if hasattr(child, 'as_sql'): --> 100 sql, params = child.as_sql(qn=qn) 101 else: 102 # A leaf node in the tree. /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in as_sql(self, qn) 98 try: 99 if hasattr(child, 'as_sql'): --> 100 sql, params = child.as_sql(qn=qn) 101 else: 102 # A leaf node in the tree. /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in as_sql(self, qn) 98 try: 99 if hasattr(child, 'as_sql'): --> 100 sql, params = child.as_sql(qn=qn) 101 else: 102 # A leaf node in the tree. /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in as_sql(self, qn) 101 else: 102 # A leaf node in the tree. --> 103 sql, params = self.make_atom(child, qn) 104 105 except EmptyResultSet: /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in make_atom(self, child, qn) 148 if isinstance(lvalue, tuple): 149 # A direct database column lookup. --> 150 field_sql = self.sql_for_columns(lvalue, qn) 151 else: 152 # A smart object with an as_sql() method. /usr/lib/python2.4/site-packages/django/db/models/sql/where.pyc in sql_for_columns(self, data, qn) 200 table_alias, name, db_type = data 201 if table_alias: --> 202 lhs = '%s.%s' % (qn(table_alias), qn(name)) 203 else: 204 lhs = qn(name) /usr/lib/python2.4/site-packages/django/db/models/sql/query.pyc in quote_name_unless_alias(self, name) 173 self.quote_cache[name] = name 174 return name --> 175 r = self.connection.ops.quote_name(name) 176 self.quote_cache[name] = r 177 return r /usr/lib/python2.4/site-packages/django/db/backends/postgresql/ operations.pyc in quote_name(self, name) 61 62 def quote_name(self, name): ---> 63 if name.startswith('"') and name.endswith('"'): 64 return name # Quoting once is enough. 65 return '"%s"' % name AttributeError: 'NoneType' object has no attribute 'startswith' -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-develop...@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.