Friday, April 22, 2011

Re: Django ORM question about lookups that span relationships

On Fri, Apr 22, 2011 at 5:36 AM, Michael Radziej <mir@spieleck.de> wrote:
> In SQL, it's something along
>
> SELECT ... FROM blog
> JOIN entry e1 on e1.entry_id = blog.id
>     AND e1.pub_date <= ...
>     AND e1.headline LIKE "%Easter%"
>     AND NOT exists
>     ( SELECT id from entry e2
>       WHERE e2.entry_id = blog.id
>       AND e2.pub_date <= ...
>       AND e2.pub_date > e1.pub_date
>     )
>
> Well ... even after 5 years with Django, I still think more in SQL than
> in Django ORM. Does anyone know how to that in the ORM?

Blog.objects.raw('''SELECT * FROM blog
JOIN entry e1 on e1.entry_id = blog.id
AND e1.pub_date <= ...
AND e1.headline LIKE "%Easter%"
AND NOT exists
( SELECT id from entry e2
WHERE e2.entry_id = blog.id
AND e2.pub_date <= %s
AND e2.pub_date > e1.pub_date
)''', [pub_date])

...

No, really -- if I was faced with a query, like this, that was easier
to express with SQL, I'd reach for `raw()` long before trying to
figure if I could twist the ORM to do my bidding.

Jacob

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate