Friday, August 14, 2015

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

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b44b0d43-2499-4075-8842-8c9fb76d3160%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment