FeedJack, Django and select_related
Posted by Andrey Khavryuchenko on 28 May 2007 at 09:42 pm | Tagged as: Blog, django if (function_exists('social_bookmark')) { social_bookmark(); } ?>
Lately I wanted to collect my django feeds into a single web-viewable channel.
There’s more than a single solution to do this. Being django fellow, I’ve decided to utilize FeedJack.
So… Well, I guess, it might work flawlessly for other people, but in my cause it failed miserably after adding two feeds:
- official django blog http://www.djangoproject.com/weblog/ and
- djangosnippets http://www.djangosnippets.org/
Just after adding djangosnippets, it messed up the order of posts terribly.
What’s going on? The code shows.
FeedJack urls.py says:
urlpatterns = patterns('',
[...]
(r'^$', views.mainview),
)
That, after couple hops, translates to the fjlib.get_paginator:
def get_paginator(site, sfeeds_ids, page=0, tag=None, user=None):
""" Returns a paginator object and a requested page from it.
"""
if tag:
try:
localposts = models.Tag.objects.get(name=tag).post_set.filter(\
feed__in=sfeeds_ids)
except:
raise Http404
else:
localposts = models.Post.objects.filter(feed__in=sfeeds_ids)
if user:
try:
localposts = localposts.filter(feed=user)
except:
raise Http404
if site.order_posts_by == 2:
localposts = localposts.order_by('-date_created', '-date_modified')
else:
localposts = localposts.order_by('-date_modified')
paginator = ObjectPaginator(localposts.select_related(),
site.posts_per_page)
try:
object_list = paginator.get_page(page)
except InvalidPage:
if page == 0:
object_list = []
else:
raise Http404
return (paginator, object_list)
Quick introspection reveals that localpost order isn’t broken just until the FeedJack creates the paginator with:
paginator = ObjectPaginator(localposts.select_related(),
site.posts_per_page)
After that, the post order is messed up and the cause is optimizing call to select_related.
I’m not yet sure if it’s a feature or a bug, but certainly, the premature optimization is the root of all evils.
- Django - unicode branch
- python 2.4
- FeedJack 0.9.9
Good luck!
PS. Finally it was solved by leaving select_related in and adding sorting by id to preserve the original order.
Leave a Reply
You must be logged in to post a comment.







