Django

Code

Ticket #3691 (closed: duplicate)

Opened 1 year ago

Last modified 1 year ago

Telling Q Objects to split up foreign keys (extension of #3592)

Reported by: axiak@mit.edu Assigned to: adrian
Milestone: Component: Database wrapper
Version: SVN Keywords: Q,QSplit,m2m
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

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.

Example: 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.

Attachments

ticket_3691_axiak.diff (7.2 kB) - added by axiak@mit.edu on 03/09/07 19:41:52.
Patch to django for QSplit

Change History

03/09/07 19:41:52 changed by axiak@mit.edu

  • attachment ticket_3691_axiak.diff added.

Patch to django for QSplit

03/10/07 00:19:50 changed by mtredinnick

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to duplicate.
  • needs_tests changed.
  • needs_docs changed.

The actual bug involved here is the same as in #1801 (that one talks about whole QuerySet objects, not Q() objects, but hte underlying logic is the same).

The workaround might be useful for some people, but please put that in the wiki, rather than the ticket tracker, since otherwise there would be no way to effectively resolve this ticket.


Add/Change #3691 (Telling Q Objects to split up foreign keys (extension of #3592))




Change Properties
Action