To be honest this seems like something which would be a lot of work with
relatively little gain.  For this to work raw() would need to change from a
relatively simple bit of code which doesn't need to touch all of the complex
query code in the ORM to a complex bit of code which needs to deeply modify
the behavior of the query code.
For most use-cases where you simply wish to inject bits of SQL into the
query, the existing extra() method works well.  Otherwise you've probably
already written the query you want to use.  To use "advanced extra" type
tools you'd have to do additional work to break down your new query into
something that fits into these functions.  With a simple raw all one would
need to do is plug their query into the raw() method.
In addition to making raw() much more complicated, this proposal eliminates
or dramatically complicates the possibility of implementing raw() for
non-relational back-ends.

If somebody really wants this functionality and can provide an
implementation it could certainly be looked at more deeply.  Otherwise IMHO
this would not be a good thing to add to raw() and have little interest in
implementing it.

____________________________
Sean O'Connor
http://seanoc.com


On Fri, Oct 2, 2009 at 7:35 AM, mrts <mrts.py...@gmail.com> wrote:

>
> 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