Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#28418 closed Bug (fixed)

Creating join paths to fields that are GenericRelations to proxy models fails with an AttributeError

Reported by: Rachel Tobin Owned by: nobody
Component: contrib.contenttypes Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Rachel Tobin)

This appears to be a regression introduced by https://code.djangoproject.com/ticket/12885. When attempting to build joins for a GenericRelation field that points to a proxy model, an AttributeError is caused via contrib/contenttypes/fields.py:375 , when trying to get the ancestor of the proxy model's parent. Since the proxy model's parent does not have an ancestor, None is returned and contrib/contenttypes/fields.py:377 raises an AttributeError, given that field is None.

As an example:

class GenericConcreteModel(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey()


class GenericProxyModel(GenericConcreteModel):
    class Meta:
        proxy = True


class GenericRelationProxyModel(models.Model):
    proxy_model = GenericRelation('GenericProxyModel')

Where the query

GenericRelationProxyModel.objects.filter(proxy_model__content_type=ContentType.objects.get(id=1))

will cause an attribute error.

I have made a patch and submitted the PR https://github.com/django/django/pull/8792 to fix this issue.

Change History (4)

comment:1 Changed 20 months ago by Rachel Tobin

Description: modified (diff)

comment:2 Changed 20 months ago by Simon Charette

Triage Stage: UnreviewedAccepted
Version: 1.11master

comment:3 Changed 20 months ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In f9e5f9a:

Fixed #28418 -- Fixed queryset crash when using a GenericRelation to a proxy model.

comment:4 Changed 20 months ago by Tim Graham <timograham@…>

In 801b6fb3:

[1.11.x] Fixed #28418 -- Fixed queryset crash when using a GenericRelation to a proxy model.

Backport of f9e5f9ae9f83c7ddf5e5d3c369b6bf54a9b80ab5 from master

Note: See TracTickets for help on using tickets.
Back to Top