Skip to content

Instantly share code, notes, and snippets.

@Enome
Created January 31, 2012 14:41
Show Gist options
  • Save Enome/1710834 to your computer and use it in GitHub Desktop.
Save Enome/1710834 to your computer and use it in GitHub Desktop.
from django.core.management import setup_environ
import settings
setup_environ(settings)
from django.db import connection
from apps.books.models import Book
books = Book.objects.raw( """
select *
from books_book
where
genre_id in
(select genre_id
from books_book
where
id in (select book_id from books_bookrelation where user_id = 564 and connection = 2))
or id in
(select book_id
from books_book_authors
where author_id in
(select author_id
from books_book_authors
where book_id in (select book_id from books_bookrelation where user_id = 564 and connection = 2)))
and id not in
(select book_id from books_bookrelation where user_id = 564 and connection = 2)
and cover is not null
limit 10
offset 10
""")
len(list(books))
print 'test', connection.queries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment