Django get users with userprofiles -
how can users userprofiles?
i have own comments app
model:
apps = ( (1, 'game'), (2, 'article'), (3, 'cms'), (4, 'user profile'), ) comment_type = models.integerfield(choices=apps, default=1, verbose_name="comment type") object_id = models.integerfield(default=0) user = models.foreignkey(user, null = true, blank = false, related_name='user')
view:
context = { ... 'comments': comment.objects.filter(comment_type=1, object_id=game_id), ... }
userprofile:
class userprofile(models.model): user = models.onetoonefield(user) signature = models.textfield(blank=true)
and view:
{% comment in comments %} .... {{ comment.user.profile.signature }} {% endfor %}
i django rookie. thank you.
you can use .select_related() here, comment has foreign key user, , user has foreign key profile:
comments = comment.objects.filter( comment_type=1, object_id=game.id).select_related('user__userprofile')
.prefetch_related() separate lookup each query. .select_related() grabs in 1 statement.
Comments
Post a Comment