﻿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
17424	Using exclude on a queryset with an annotate field gives attribute error.	joseph.helfer@…	Łukasz Rekucki	"Generating a MySQL query with an `annotate` followed by an `exclude` which follows two foreignkey relations results in:
{{{
AttributeError: 'NoneType' object has no attribute 'startswith'
}}}

To reproduce this, you can do the following:
Create a new project, set the backend to `mysql` (and make a database), and create an app `foo` with this `models.py`:

{{{#!python
from django.db import models

class Model1(models.Model):
    pass

class Model2(models.Model):
    model1 = models.ForeignKey(Model1)

class Model3(models.Model):
    model2 = models.ForeignKey(Model2)
}}}

Put `foo` in `INSTALLED_APPS`, run `syncdb`, and then run this in the django shell

{{{#!python
from foo.models import *
from django.db.models import Count

print Model3.objects.annotate(bar=Count('id')).exclude(model2__model1__id=4).query
}}}


I'm running django 1.3.1 and python 2.6.7 on Mac OSX 10.7.1

This seems to be very similar to
https://code.djangoproject.com/ticket/12687
but, although that ticket is closed, the bug persists."	Bug	closed	Database layer (models, ORM)	1.3	Normal	fixed		asendecka@…	Ready for checkin	1	0	0	0	0	0
