﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36954	Filtering an ManyToManyField causes the `.values` call to be filtered	"Ryan ""RB"" Barnes"	Mahi Singhal	"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.
"	Bug	closed	Database layer (models, ORM)	5.1	Normal	invalid			Unreviewed	0	0	0	0	0	0
