Re: [Django] #36135: Prefetching a reverse GenericRelation raises AttributeError

36 views
Skip to first unread message

Django

unread,
Jan 25, 2025, 12:38:02 AM1/25/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: prefetch | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* owner: (none) => Simon Charette
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 31, 2025, 6:11:49 AM1/31/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: prefetch | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_tests: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:4>

Django

unread,
Jan 31, 2025, 10:21:44 AM1/31/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: prefetch | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* needs_tests: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:5>

Django

unread,
Feb 3, 2025, 3:38:14 AM2/3/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution:
Keywords: prefetch | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:6>

Django

unread,
Feb 3, 2025, 4:39:23 AM2/3/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution: fixed
Keywords: prefetch | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"198b30168d4e94af42e0dc7967bd3259b5c5790b" 198b301]:
{{{#!CommitTicketReference repository=""
revision="198b30168d4e94af42e0dc7967bd3259b5c5790b"
Fixed #36135 -- Fixed reverse GenericRelation prefetching.

The get_(local|foreign)_related_value methods of GenericRelation must be
reversed because it defines (from|to)_fields and associated related_fields
in the reversed order as it's effectively a reverse GenericForeignKey
itself.

The related value methods must also account for the fact that referenced
primary key values might be stored as a string on the model defining the
GenericForeignKey but as integer on the model defining the
GenericRelation.
This is achieved by calling the to_python method of the involved content
type
in get_foreign_related_value just like GenericRelatedObjectManager does.

Lastly reverse many-to-one manager's prefetch_related_querysets should use
set_cached_value instead of direct attribute assignment as direct
assignment
might are disallowed on ReverseManyToOneDescriptor descriptors. This is
likely
something that was missed in f5233dc (refs #32511) when the is_cached
guard
was added.

Thanks 1xinghuan for the report.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:7>

Django

unread,
Feb 3, 2025, 4:41:54 AM2/3/25
to django-...@googlegroups.com
#36135: Prefetching a reverse GenericRelation raises AttributeError
-------------------------------------+-------------------------------------
Reporter: john | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: | Version: 5.1
contrib.contenttypes |
Severity: Normal | Resolution: fixed
Keywords: prefetch | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce <42296566+sarahboyce@…>):

In [changeset:"303c2569dad4e3222e94fc5b76dc7b83b35fde17" 303c256]:
{{{#!CommitTicketReference repository=""
revision="303c2569dad4e3222e94fc5b76dc7b83b35fde17"
[5.2.x] Fixed #36135 -- Fixed reverse GenericRelation prefetching.

The get_(local|foreign)_related_value methods of GenericRelation must be
reversed because it defines (from|to)_fields and associated related_fields
in the reversed order as it's effectively a reverse GenericForeignKey
itself.

The related value methods must also account for the fact that referenced
primary key values might be stored as a string on the model defining the
GenericForeignKey but as integer on the model defining the
GenericRelation.
This is achieved by calling the to_python method of the involved content
type
in get_foreign_related_value just like GenericRelatedObjectManager does.

Lastly reverse many-to-one manager's prefetch_related_querysets should use
set_cached_value instead of direct attribute assignment as direct
assignment
might are disallowed on ReverseManyToOneDescriptor descriptors. This is
likely
something that was missed in f5233dc (refs #32511) when the is_cached
guard
was added.

Thanks 1xinghuan for the report.

Backport of 198b30168d4e94af42e0dc7967bd3259b5c5790b from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36135#comment:8>
Reply all
Reply to author
Forward
0 new messages