#28921: On filtering nested many to many fields
-------------------------------------+-------------------------------------
               Reporter:  Sassan     |          Owner:  nobody
  Haradji                            |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  2.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  manytomany query
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28921>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.5037b83e3f85d532a58a764512bf53a3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to