Sorry, here's the example.

Or is there any simpler way to achieve the same thing?

JJ.

class Book(models.Model):
    title = ...
    main_author = ForeignKey(Author, null=True)
    co_authors = ManyToManyField(Author)

class Reprint(models.Model):
    book = ForeignKey(Book)

class BooksManager(models.Manager):
    def select_related_ex(self, query=None):
        books = query.select_related()
        main_authors = self.main_authors_in_bulk()
        co_authors = self.main_authors_in_bulk()
        reprints = self.reprints_in__bulk()
        for book in books:
            book.cached_main_author = main_authors.get(book.id, None)
            book.cached_co_authors = co_authors.get(book.id, None)
            book.cached_reprints = reprints.get(book.id, None)

   def main_authors_in_bulk(self):
        query = """
        SELECT book_book.id, book_main_author.first_name,
book_main_author.last_name
        FROM book_book, book_main_author
        WHERE
            book_book.book_id = book_main_author.initials
        """
        cursor = db.connection.cursor()
        cursor.execute(query)
        values = {}
        for id, first_name, last_name in cursor.fetchall():
            values[id] = mymodel.Author(first_name=first_name,
last_name=last_name)
        return values

    def co_authors_in_bulk(self):
        query = """
        SELECT
            book_book_co_authors.book_id,
            book_author.initials, book_author.first_name,
book_author.last_name
        FROM book_book_co_authors, book_author
        WHERE
            book_book_co_authors.author_id = book_author.initials
        ORDER BY
            book_author.initials ASC
        """
        cursor = db.connection.cursor()
        cursor.execute(query)
        values = {}
        for id, first_name, last_name in cursor.fetchall():
            ids = values.get(id, [])
            ids.append(mymodel.Author(first_name=first_name,
last_name=last_name))
            values[id] = ids
        return values

    def reprints_in_bulk():
         ...

On Jul 13, 12:26 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Fri, 2007-07-13 at 09:57 +0000, jj wrote:
> > I posted earlier about performance issues trying to display many books
> > in one single page. I've since fixed the problems by reducing the
> > number of SQL queries from 12 per book, to just 11 overall. I also
> > perform filtering based on user's permissions up-front. Big
> > performance boost.
>
> > I ended writing several managers. Typing almost the same code in each
> > manager. Encapsulating it all in a hand-crafted select_related_ex()
> > that also fetches ManyToManyField's and ForeignKey's where
> > "null=True".
>
> > It occurred to me that such code could/should probably be made generic
> > and part of the standard Django distribution.
>
> You haven't included an example of what you're talking about, so it's
> difficult to comment on the specifics.
>
> Malcolm
>
> --
> Telepath required. You know where to apply...http://www.pointy-stick.com/blog/


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

Reply via email to