﻿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
28921	On filtering nested many to many fields	Sassan Haradji	nobody	"Consider that related is a many to many field of Object.

If you filter `Object.objects.filter(related__a=1, related__b=2)` a=1 and b=2 conditions apply to the same related but if you filter `Object.objects.filter(related__a=1).exclude(related__b=2)` it returns Object instances that have a related with a = 1 and they have no related that has b = 2. It doesn't provide a way to filter Object instances that have a related with a = 1 which at the same time has b != 2.

Providing `__not...` for each lookup is one way to solve it. But it still keeps the functionality limited for many to many fields. If user needs all Object instances which have 
1. An related object that has a=1 and b=2
and
2. Another or same related object that has b=3 and c!=4
it can't be done with above.
To achieve this we need a way to categorize many to many field object and apply conditions to different categories.
One way to do so is to write it like this:
{{{
Object.objects.filter(related__1__a=1, related__1__b=2, related__2__b=3).exclude(related__2__c=4)
}}}
The role is to consider the optional number after each many to many field in queries, a category for that many to many field. It is to categorize conditions.

I doubt if filter method in Django has any idea about state of exclude or vice versa so I think implementing above in Django is hard. And making filter and exclude to relate is not intuitive too. Another solution is a combination of above solutions (`__not...` lookups and lookup categories):
{{{
Object.objects.filter(related__1__a=1, related__1__b=2, related__2__b=3, related__2__c__neq=4)
}}}"	Uncategorized	closed	Database layer (models, ORM)	2.0	Normal	invalid	manytomany query		Unreviewed	0	0	0	0	0	0
