views:

91

answers:

2

I'm using the django comments framework, and when I list the comments I want to include some of the information stored in auth_user. However, I find I need an extra query for each comment to get the user info.

I tried using select_related() when I pull the comments, but this doesn't help.

Is there a reason that it's not joining the auth_user table, and is there any way to force it to?

A: 

here is a snip from the original django comments model:

    user        = models.ForeignKey(User, verbose_name=_('user'),
                    blank=True, null=True, related_name="%(class)s_comments")
    user_name   = models.CharField(_("user's name"), max_length=50, blank=True)
    user_email  = models.EmailField(_("user's email address"), blank=True)
    user_url    = models.URLField(_("user's URL"), blank=True)

so you can use comment.user to get your user and/or user.users_comments to get all comments of your user:-)

renton
Let's say I want comment.user.is_staff. When I request that, it's going to need another db query to get that info. If one user has 10 comments on the page, it's going to do the same auth_user query 10 times. That's what I'm trying to prevent.
AlexH
A: 

You could get all of the users that have comments on your page then dump them into a dictionary. The you can pull user data from your dictionary while looping over the comments.

Mark Kecko