[bump] Preventing JOIN while checking if a self referencing FK is null

25 views
Skip to first unread message

diafygi

unread,
Mar 21, 2012, 4:41:32 AM3/21/12
to Django users
There is an old thread that didn't end in a resolution about
preventing joins in a filter(foreign_key=None) scenario.

http://groups.google.com/group/django-users/browse_thread/thread/61ee2fb22deae326

I'd like to bring it up again and ask if there is now a way to prevent
joins from a query.

An example:
class Blog(model.Models):
editor = models.ForeignKey(User, null=True)

# find all the blogs with no editors
>>> Blog.objects.filter(editor=None)
DEBUG:django.db.backends:(0.039) SELECT `myapp_blog`.`id` FROM
`myapp_blog` LEFT OUTER JOIN `myapp_user` ON (`myapp_blog`.`editor_id`
= `myapp_user`.`id`) WHERE `myapp_user`.`id` IS NULL;

# Is there a way to do this query?
>>> Blog.objects.filter(editor=None)
DEBUG:django.db.backends:(0.039) SELECT `myapp_blog`.`id` FROM
`myapp_blog` WHERE `myapp_blog`.`editor_id` IS NULL;

Thanks!
Daniel

Javier Guerra Giraldez

unread,
Mar 21, 2012, 9:47:49 AM3/21/12
to django...@googlegroups.com
try:

> Blog.objects.filter(editor_id=None)

--
Javier

diafygi

unread,
Mar 21, 2012, 12:05:37 PM3/21/12
to Django users
>>> Blog.objects.filter(editor_id=None)
FieldError: Cannot resolve keyword 'editor_id' into field.

This was actually an offered answer in the previous thread, but the id
version of the field still raises a field error.

Daniel

On Mar 21, 9:47 am, Javier Guerra Giraldez <jav...@guerrag.com> wrote:
> try:
>
> > Blog.objects.filter(editor_id=None)
>
> --
> Javier

Andre Terra

unread,
Mar 21, 2012, 2:07:08 PM3/21/12
to django...@googlegroups.com
On Wed, Mar 21, 2012 at 5:41 AM, diafygi <dia...@gmail.com> wrote:
>>> Blog.objects.filter(editor=None)


>>> print Blog.objects.filter(editor=None).values('id').query
SELECT "myapp_blog"."id" FROM "myapp_blog" WHERE "myappblog"."editor_id" IS NULL


Cheers,
AT

Python_Junkie

unread,
Mar 21, 2012, 2:14:04 PM3/21/12
to django...@googlegroups.com
Use straight sql.

There are always potentially limitations with an abstraction such as the ORM.

Why try to fit a square peg into a round whole.

Andre Terra

unread,
Mar 21, 2012, 2:31:49 PM3/21/12
to django...@googlegroups.com
Because django offers a nice ORM that makes it easy to do most queries, especially one-liners like this one.

And refactoring raw SQL is a royal PITA.


Cheers,
AT

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NfvXnYxnHBkJ.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

diafygi

unread,
Mar 21, 2012, 5:42:14 PM3/21/12
to Django users
I cannot confirm that behavior. Can others verify?

I'm using Django 1.3 and I get:
>>> print Blog.objects.filter(editor=None).values('id').query
SELECT `myapp_blog`.`id` FROM `myapp_blog` LEFT OUTER JOIN
`myapp_user` ON (`myapp_blog`.`editor_id` = `myapp_user`.`id`) WHERE
`myapp_user`.`id` IS NULL

-Daniel

On Mar 21, 2:07 pm, Andre Terra <andrete...@gmail.com> wrote:

Andre Terra

unread,
Mar 21, 2012, 5:52:07 PM3/21/12
to django...@googlegroups.com

Sorry. In the process of changing model names to fit my own installation and test the code, I also managed to invert the relation.

Pay no attention to the man behind the curtains!

Cheers,
AT

--
You received this message because you are subscribed to the Google Groups "Django users" group.

diafygi

unread,
Mar 22, 2012, 7:15:50 PM3/22/12
to Django users
Is there a reason why editor_id is meant to raise a field error?

-Daniel
Reply all
Reply to author
Forward
0 new messages