[Django] #36954: Filtering an ManyToManyField causes the `.values` call to be filtered

0 views
Skip to first unread message

Django

unread,
1:21 AM (10 hours ago) 1:21 AM
to django-...@googlegroups.com
#36954: Filtering an ManyToManyField causes the `.values` call to be filtered
-------------------------------------+-------------------------------------
Reporter: Ryan "RB" Barnes | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Given two following tables:


{{{
class Technician(models.Model):
...

class Appointment(models.Model):
technicians = models.ManyToManyField(Technician)
}}}


If a have an appointment with two technicians assigned to it and create
the follow queries I get results I would not expect

{{{
tech_1 = Technician()
tech_1.save()
tech_2 = Technician()
tech_2.save()

appointment = Appointment(technicians=[tech_1, tech_2])
appointment.save()

# No issues here
r1 =
Appointment.objects.filter(id=appointment.id).values_list('technicians',
flat=True)
assert tech_1.id in r1
assert tech_2.id in r1

# Now filter for a single technician
r2 = Appointment.objects.filter(id=appointment.id,
technicians__in=[tech_1]).values_list('technicians', flat=True)
assert tech_1.id in r2

# Fails here as tech_2 has been filtered out
assert tech_2.id in r2
}}}

This is certainly because the SQL produced joins against the through
model, but then filters it directly while also selecting from it.

I know I can get around this but using subqueries and ArrayAggs in various
ways but it feels like a footgun that most users would miss.
--
Ticket URL: <https://code.djangoproject.com/ticket/36954>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
3:09 AM (8 hours ago) 3:09 AM
to django-...@googlegroups.com
#36954: Filtering an ManyToManyField causes the `.values` call to be filtered
-------------------------------------+-------------------------------------
Reporter: Ryan "RB" Barnes | Owner: Mahi
| Singhal
Type: Bug | Status: assigned
Component: Database layer | Version: 5.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mahi Singhal):

* owner: (none) => Mahi Singhal
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36954#comment:1>
Reply all
Reply to author
Forward
0 new messages