Is it possible to get object with comments related to it? Right now django comment framework creates query for every object which has related comments and another queries for comments owners. Can I somehow avoid this? I use django 1.4 so prefetch_related is allowed.
Share
You could create a function that caches the count:
You could extend the Comment model and add get_comment_count there. Or put get_comment_count as a template filter. It doesn’t matter.
Of course, you would also need cache invalidation when a new comment is posted:
You could improve this last snippet, by using cache.incr() on comment_was_posted, and cache.decr() on post_delete but that’s left as an exercise for you 🙂