On Oct 3, 6:04 pm, Luke Plant <l.plant...@cantab.net> wrote:
> Hi all,
>
> The patch for this is now ready, as far as I'm concerned, but I'd like
> to bring it up here again before committing, mainly because Alex Gaynor
> expressed some doubts.

I have done a review of the patch, and I think it is commit ready.
There has been some bug squashing, but I feel it is very good at the
moment. Hope to see it soon in trunk!

About merging prefetch_related and select_related: That does make
sense. APIs are not my strongest area of expertise, so I wont try to
discuss about this more.

> Anssi is planning further extensions, but I think these should wait
> until the main functionality has been adopted.

The real reason for this post is that I would like to talk already at
this point about the extensions I have planned and also about the API
for the extensions. There are two extension currently planned:
 - Allow fetching into different variable than .related_manager.all(),
done by:
Book.objects.prefetch_related(R('authors',
to_attr='authors_prefetched'))
 - Allow usage of custom queryset
qs = Book.objects.prefetch_related(
    R('authors', to_attr='young_authors',
qs=Author.objects.filter(age__lte=30)).order_by('-age')
)
Now qs[0].young_authors will have all the authors of the first book in
the qs ordered by age.

There is also the possibility to use prefetched objects in a chain:

qs = Author.objects.prefetch_related(
    R('books', to_attr='django_books',
qs=Book.objects.filter(name__icontains='django')),
    R('django_books__authors', to_attr='young_authors',
qs=Author.objects.filter(age__lte=30))
)
Now qs[0].django_books will have all the books about Django written by
the author, and qs[0].django_books[0].young_authors will have all the
young authors of the first django book in django_books.

Using this feature it would be pretty easy to fetch discussion threads
and for each thread posts made today in two SQL queries by just
issuing .prefetch_related. Or even fetch forums, today's threads, and
all the posts made today per thread in three SQL queries, again by
just issuing .prefetch_related.

Fetching latest n posts per thread is hard to do efficiently in SQL
but that would be a killer feature. Window functions can do that, but
they are not efficient (at least not in PostgreSQL) and not available
in SQLite or MySQL.

But I am (once again) getting carried away. There is a working patch
in the ticket mentioned above implementing the features. Although it
is a bit stale compared to the latest work in the ticket. I will
create a separate ticket for this when prefetch_related lands trunk.

I hope to get the extensions included in 1.4. When is feature freeze
coming?

Thoughts?

 - Anssi

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

Reply via email to