Possible Bug when using exclude on a GenericForeignKey / GenericRelation query.

159 views
Skip to first unread message

Daniel H

unread,
Aug 14, 2015, 5:01:30 PM8/14/15
to Django users
Hi everyone.

I've been using django for almost a year now, but it's my first time posting here.

Anyway, I'm encountering an error which I think might be a bug. I've yet to try and replicate it as I thought posting here would be the better first step, as I might just be doing something incorrectly.

When I try a query with .exclude() on a table with a GenericForeignKey and a reverse Generic Relation on a different table, I get this:

AttributeError at /list
'GenericRelation' object has no attribute 'field'

Here is a mock up of my database structure:

class InternalOffer(models.Model):
    user = models.ForeignKey('User')
    status = models.CharField(max_length=64)
    (more offer data...)
    relation = GenericRelation(OfferJoin, related_query_name='offerquery')

class ExternalOffer(models.Model):
    user = models.ForeignKey('User')
    status = models.CharField(max_length=64)
    (more offer data...)
    relation = GenericRelation(OfferJoin, related_query_name='offerquery')

class OfferJoin(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    offer = GenericForeignKey('content_type', 'object_id')

I can filter fine, it's only the exclude that causes errors. Here is the code that causes the error:

offers = OfferCover.objects.filter(offerquery__user__id=request.user.id)
offers = offers.exclude(offerquery__status="Completed")

Here's the full callback:

Django Version: 1.8.2
Python Version: 2.7.6

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/Users/path-to-project/project/views.py" in list
  136.     offers = offers.exclude(o__status="Completed")
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in exclude
  686.         return self._filter_or_exclude(True, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  695.             clone.query.add_q(~Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
  1304.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1332.                     allow_joins=allow_joins, split_subq=split_subq,
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1180.                                       can_reuse, e.names_with_path)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in split_exclude
  1562.         trimmed_prefix, contains_louter = query.trim_start(names_with_path)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in trim_start
  1989.         join_field = path.join_field.field

Exception Type: AttributeError at /list
Exception Value: 'GenericRelation' object has no attribute 'field'

I've read the documentation multiple times and haven't come across anything usefull. I know that filtering GFKs alone won't work, but both filtering and excluding should work with the reverse Generic Relation.

Thanks!
- Daniel Harris

Tim Graham

unread,
Feb 22, 2016, 1:03:31 PM2/22/16
to Django users
The crash is probably a bug: https://code.djangoproject.com/ticket/26261
Reply all
Reply to author
Forward
0 new messages