[Django] #20923: Unable to use fields from prefetch_related() in only() and defer()

45 views
Skip to first unread message

Django

unread,
Aug 14, 2013, 8:47:56 PM8/14/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Keywords: defer only ForeignKey
Severity: Normal | reverse relationship
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
If I run the following query, I get the same error as seen in #14694:

{{{
ParentModel.objects.prefetch_related('child_model').only('parent_attribute',
'childs__child_atrtibute').all()
}}}

I guess it is not really a bug, as using `only()` and `defer()` in
combination with `prefetch_related()` is not documented (documentation
only mentions the use of `select_related()` with `defer()`).

But it could be a useful feature: is there a reason to support
`select_related()` but not `prefetch_related()`?

I hope that an improvement of the patch for #14694 can enable this
feature.

P.S. I wasn't able to fix it by myself, due to my lack of understanding of
how `django.db.models.sql.query.deferred_to_data()` works. Simply removing
`and field.field.unique` in `django.db.models.sql.query.is_reverse_o2o()`
makes the error message disappear, but the instructions in `only()` remain
ignored.

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

Django

unread,
Aug 14, 2013, 8:48:24 PM8/14/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: defer only | Unreviewed
ForeignKey reverse relationship | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by as@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20923#comment:1>

Django

unread,
Aug 14, 2013, 8:51:52 PM8/14/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: defer only | Unreviewed
ForeignKey reverse relationship | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by alexpirine):

* cc: as@… (added)


Comment:

I registered to follow this ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/20923#comment:2>

Django

unread,
Aug 23, 2013, 7:34:42 AM8/23/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: defer only | Unreviewed
ForeignKey reverse relationship | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by akaariai):

I think a better way is to add custom query support to prefetch_related
(or, #17001). Main reason is that I believe implementation of
communicating the only/defer data from the main query to the prefetch
queries will be hard to do cleanly.

--
Ticket URL: <https://code.djangoproject.com/ticket/20923#comment:3>

Django

unread,
Aug 23, 2013, 7:35:37 AM8/23/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution: wontfix

Severity: Normal | Triage Stage:
Keywords: defer only | Unreviewed
ForeignKey reverse relationship | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by akaariai):

* status: new => closed
* resolution: => wontfix


Comment:

Forgot to wontfix this...

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

Django

unread,
Aug 23, 2013, 8:55:40 AM8/23/13
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 1.5
(models, ORM) | Resolution: wontfix
Severity: Normal | Triage Stage:
Keywords: defer only | Unreviewed
ForeignKey reverse relationship | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by alexpirine):

Replying to [comment:3 akaariai]:


> I think a better way is to add custom query support to prefetch_related
(or, #17001). Main reason is that I believe implementation of
communicating the only/defer data from the main query to the prefetch
queries will be hard to do cleanly.

Thanks, it looks interesting. If I understood it correctly, I could pass
the only() attribute to the custom queryset:

{{{
ParentModel.objects.prefetch_related(
R('child_model', to_attr = 'childs', qs =
ChildModel.objects.only('child_attribute')
).only('parent_attribute').all()
}}}

which would answer this issue.

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

Django

unread,
May 10, 2024, 3:28:35 PM5/10/24
to django-...@googlegroups.com
#20923: Unable to use fields from prefetch_related() in only() and defer()
-------------------------------------+-------------------------------------
Reporter: alexpirine | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 1.5
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: defer only | Triage Stage:
ForeignKey reverse relationship | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Natalia Bidart):

#35442 may be a duplicate of this one.
--
Ticket URL: <https://code.djangoproject.com/ticket/20923#comment:6>
Reply all
Reply to author
Forward
0 new messages