Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
defining outer joins
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Michael Radziej  
View profile  
 More options Oct 17 2006, 8:40 am
From: Michael Radziej <m...@noris.de>
Date: Tue, 17 Oct 2006 14:40:26 +0200
Local: Tues, Oct 17 2006 8:40 am
Subject: defining outer joins
Hi,

I was looking for an easy way to define extra outer joins. It
turned out that it's easy to extend the QuerySet.extra method
with a 'joins' argument used like this:

notes = Note.objects.select_related().extra(
   joins=['''left outer join %s rel1 on
             rel1.id=%s.release_beginn_id'''
          % (Release._meta.db_table, Note._meta.db_table),
          '''left outer join %s rel2  on
             rel2.id=%s.release_behoben_id'''
          % (Release._meta.db_table, Note._meta.db_table)],
   where=['''((%(notes_table)s.release_beginn_id is null or
                   rel1.sort_value <= %(sort_value)d)
              and (%(notes_table)s.release_behoben_id is null or
                   rel2.sort_value > %(sort_value)d))'''
                 % {'sort_value': version, 'notes_table':
                       Note._meta.db_table}])

(I left out the db name quoting since it doesn't contribute much
to this example.)

The patch is surprisingly small:

http://code.djangoproject.com/attachment/ticket/2922/extra_joins.diff

I guess this is currently under a freeze. I just wanted to bring
it up for discussion. If there's serious interest, I can flesh it
out (docs, testcases) later when the winter^H^H^H^H^H^H^H freeze
is over.

Michael


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Russell Keith-Magee  
View profile  
 More options Oct 17 2006, 8:28 pm
From: "Russell Keith-Magee" <freakboy3...@gmail.com>
Date: Wed, 18 Oct 2006 08:28:23 +0800
Local: Tues, Oct 17 2006 8:28 pm
Subject: Re: defining outer joins
On 10/17/06, Michael Radziej <m...@noris.de> wrote:

> I guess this is currently under a freeze. I just wanted to bring
> it up for discussion. If there's serious interest, I can flesh it
> out (docs, testcases) later when the winter^H^H^H^H^H^H^H freeze
> is over.

You can already specify the join type using Q objects.

When you invoke get_sql on a Q object, it returns a tuple, the first
argument of which is 'joins', a sorted dictionary that describes the
joined table name, join type, and join condition.

It's not a heavily advertised feature, so there isn't much
documentation; if you want an example, line 840 of query.py shows the
addition of a join to the sql results.

Using this approach also means that you can keep using Django query
syntax for most of your query, rather than just inserting a block of
SQL.

Yours,
Russ Magee


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »