On Tue, 2007-11-06 at 22:43 +0000, Matthew Fremont wrote: > What is the current thinking about whether/when the ORM will be > extended to allow formulation of correlated and dynamic subqueries > without requiring raw SQL? > > My motivation right now is that I have a few queries that are probably > best expressed using correlated "EXISTS (SELECT ...)" or "... IN > (SELECT ...)" subqueries in the where clause of the main query. For > simple cases, these kinds of subqueries could be implemented using > QuerySet.extra(), but since I want to build subqueries with > dynamically generated where clauses, it would be nice to be able to be > able to build on the work that has already been put into the > QuerySet.filter(), .exclude(), Q, etc. to create the correlated > subqueries. > > A search of the group archives turned up some discussion of > subqueries, but I did not come across anything that directly addresses > queries of this form. > > After looking through some of the code in django/db/models/query.py, I > think I could probably come up with one or two QOperator subclasses > that do what I want, but I figured it would be worthwhile to ask.
On the queryset-refactor branch, I've changed the way Q() objects are handled so that it will be easier to write your own variants on these. Basically, the Q-like object (by which I mean, any object you can pass to a filter() method) will have full access to the query that is being built and can add things to the where attribute and the columns and the tables and so on. This isn't really ready for testing yet because a lot of the Django codebase hasn't been ported in the branch (I'm focusing on getting the query stuff correct and fast before moving outwards), but I'm targeting end of the month for a testing phase. Before then I'll also document how to do this, but if you want a sneak preview, you can poke around the queryset-refactor branch. Look at django/db/models/query/sql.py, particularly Query.add_q(). I'm not quite sure how to do it yet, but one idea I'd like to implement is a way to more or less pass in a full Query object inside an object as part of a filter. It's not too hard -- it's already done for multi-column distinct counts, which need sub-queries on everything except MySQL. It's mostly a matter of working out the API. That might be post-merge work, though, since it's an addition and can be trialled without core modifications. Regards, Malcolm -- No one is listening until you make a mistake. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-developers?hl=en -~----------~----~----~----~------~----~------~--~---
