Telling Q Objects to split up foreign keys (extension of #3592)
|Reported by:||axiak@…||Owned by:||adrian|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
This is not a duplicate of #3592.
I was working with some of the things in #3592, and it seems from chatting on IRC that people really want a way to do ANDs on a m2m field without the Q objects subsuming them into one JOIN.
Suppose you have Article and Tag, where Article has a m2m relation with Tag (related_name = 'tag'). If I run:
from django.db.models.query import Q Article.objects.filter(Q(tag__value = 'A') & Q(tag__value = 'B')) > # no results
I would expect to get no results (there are no tags with both a value of 'A' and 'B').
However, I *would* like to somehow get a list of articles with tags meeting that criteria. Using the patch, you can:
from django.db.models.query import Q,QSplit Article.objects.filter(QSplit(Q(tag__value = 'A')) & QSplit(Q(tag__value = 'B'))) > # articles with both tags
So now they are split into different Tag entries.
This isn't meant to be used in django. Just for people if they need it before the queryset refactoring. Also, this seems like it would be useful in the final version, so it's here for reference.