#30124: QuerySet.only() on reverse many-to-one relationship causes extra
database
queries
-------------------------------------+-------------------------------------
Reporter: Beda Kosata | Owner: Dan Davis
Type: Bug | Status: assigned
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Dan Davis):
* status: new => assigned
* owner: nobody => Dan Davis
Comment:
I'd like some feedback of whether it is worth fixing this rather than
documenting the issue. My inclination is to fix it - the query object has
an alias_map for the join at the moment that only is called, and so
whether that's the best way to detect the reverse relationship or not,
there is certainly some way:
{{{
qs1 = ws.works.get_queryset()
qs1.query.alias_map['queries_workset'] # I put them in
tests/queries/models.py for a start
}}}
I'm not sure quite how to select the reverse relationship, here is the
join.__dict__:
{{{
{'table_name': 'queries_workset',
'parent_alias': 'queries_work',
'table_alias': 'queries_workset',
'join_type': 'INNER JOIN',
'join_cols': (('work_set_id', 'id'),),
'join_field': <django.db.models.fields.related.ForeignKey: work_set>,
'nullable': False,
'filtered_relation': None}
}}}
Least interesting part for me - I've validated this by writing a failing
test case. I'm counting queries using @override_settings(DEBUG=True), and
I'm not sure that's a good implementation strategy. But anyway, I'm more
interested in understanding how Django structures joins now that I've
gotten so good at annotate as a user ;)
--
Ticket URL: <https://code.djangoproject.com/ticket/30124#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.974312bb9c1ae5a15a9cda95aa856959%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.