﻿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
26261	QuerySet.exclude() crashes when referencing related_query_name of GenericRelation	Amir Hadi	gorodovoy9000	"Someone (Daniel H) already reported this in the Django users group: https://groups.google.com/forum/#!msg/django-users/vQcipOhI7N0/4plhTAJ7AAAJ

What we are experiencing is that when you use GenericForeignKey and GenericRelation, you can't use exclude on the QuerySet.

Here is our setup:

{{{
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models


class Insights(models.Model):
    object_id = models.PositiveIntegerField()
    content_type = models.ForeignKey(ContentType)
    parent_object = GenericForeignKey()
    first_field = models.IntegerField()
    second_field = models.IntegerField()


class ModelA(models.Model):
    name = models.CharField(max_length=40)

    insights = GenericRelation('Insights', related_query_name='a')


class ModelB(models.Model):
    title = models.CharField(max_length=40)

    insights = GenericRelation('Insights', related_query_name='b')

}}}

and here is how to reproduce this bug:
{{{
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type ""help"", ""copyright"", ""credits"" or ""license"" for more information.
(InteractiveConsole)
>>> from app.models import *
>>> a = ModelA.objects.create(name='Test')
>>> a = ModelA.objects.create(name='Test2')
>>> b = ModelB.objects.create(title='B Test')
>>> i = Insights.objects.create(parent_object=a, first_field=1, second_field=2)
>>> i = Insights.objects.create(parent_object=a, first_field=1, second_field=2)
>>> i = Insights.objects.create(parent_object=b, first_field=1, second_field=2)
>>> Insights.objects.count()
3
>>> i = Insights.objects.filter(a__name='Test2')
>>> i
[<Insights: Insights object>]
>>> i = Insights.objects.filter(a__name='Test2').exclude(a__name='Test')
Traceback (most recent call last):
  File ""<console>"", line 1, in <module>
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/query.py"", line 797, in exclude
    return self._filter_or_exclude(True, *args, **kwargs)
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/query.py"", line 806, in _filter_or_exclude
    clone.query.add_q(~Q(*args, **kwargs))
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/sql/query.py"", line 1243, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/sql/query.py"", line 1269, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/sql/query.py"", line 1181, in build_filter
    can_reuse, e.names_with_path)
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/sql/query.py"", line 1499, in split_exclude
    trimmed_prefix, contains_louter = query.trim_start(names_with_path)
  File ""/Users/ahadi/.virtualenvs/django/lib/python3.4/site-packages/django/db/models/sql/query.py"", line 1934, in trim_start
    join_field = path.join_field.field
AttributeError: 'GenericRelation' object has no attribute 'field'
}}}
We have the problem with Django version 1.8.9, but also reproduced it with Django 1.9.2.

A demo project is attached."	Bug	closed	contrib.contenttypes	1.8	Normal	fixed	GenericRelation exclude	steve@…	Ready for checkin	1	0	0	0	0	0
