Wishful thinking follows.

It would be awesome if one could mix ordinary QuerySet methods
with raw() (or, rather, raw_extra(), see below for that).

Assuming the following models:

class Foo(models.Model):
    name = models.CharField(max_length=255)

class FooDates(models.Model):
    foo = models.ForeignKey(Foo)
    start = models.DateField()
    end = models.DateField()

I for one would be definitely struck with awe if
I could write something in the lines of:

RAW_ORDER_BY = """
ORDER BY (SELECT MIN("start")
    FROM "myapp_foodates"
    WHERE "myapp_foodates"."start" >= %s
    AND "myapp_foo.id" = "myapp_foodates"."foo_id")
"""

q = Foo.objects.filter(
        foodate__end__gte=datetime.date.now())\
        .raw(RAW_ORDER_BY, params=(datetime.date.now(),))\
        .distinct()

and q.query.as_sql() would look as follows:

SELECT DISTINCT ...
  FROM "myapp_foo"
  INNER JOIN "myapp_foodates"
  ON ("myapp_foo"."id" = "myapp_foodates"."foo_id")
  WHERE "myapp_foodates"."end" >= "2009-10-02"
  ORDER BY (
   SELECT MIN("start")
     FROM "myapp_foodates"
     WHERE "myapp_foodates"."start" >= "2009-10-02"
       AND "myapp_foo"."id" = "myapp_foodates"."foo_id");

However, I assume that achieving this would be extremely
hard with plain raw().

What probably would work, however, is an extra()-like
raw_extra() that would accept raw parametrized strings
for each of `select`, `where` and `order_by` and plainly
replace the corresponding part in the query builder.

I.e. the example above would read:

q = Foo.objects.filter(
        foodate__end__gte=datetime.date.now())\
        .raw_extra(order_by=RAW_ORDER_BY,
                params=(datetime.date.now(),))\
        .distinct()

Best,
Mart Sõmermaa
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to