在Django中显示UserProfile的注释数

| 如何将UserProfile模型连接到用户的注释?在我的UserProfileAdmin中,我想显示每个用户的评论数。有任何想法吗? 谢谢! 编辑:我的模型:
class UserProfile(models.Model):
    GENDER_CHOICES = (
                      (\'M\', _(\'Male\')),
                      (\'F\', _(\'Female\')))
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=50)
    gender = models.CharField(max_length=2, choices=GENDER_CHOICES, blank=True, null=True)
    birthdate = models.DateField(blank=True, null=True)
    website = models.URLField(blank=True)
    description = models.TextField(blank=True)
我还想在查看UserProfile时显示用户的评论列表。由于我使用的是Django的内置注释系统,是否有执行此操作的快捷方式?还是我必须做类似ѭ1的事情?     
已邀请:
UserProfile.user.comment_comments.count()
应该这样做。 您可以将其包装到个人档案模型中的方法中
def count_comments(self):
    if self.user:
        return self.user.comment_comments.count()
并将“ 4”添加到ModelAdmin中的list_display选项。 如果要获取用户评论的列表,可以相应地调整该方法:
def get_comments(self):
    if self.user:
        return self.user.comment_comments.all()
然后,您可以执行以下操作:
User.objects.get(pk=1).comment_comments.all()
要么:
UserProfile.objects.get(pk=1).get_comments()
    
在注册管理员(或创建自定义管理员)时,只需使用诸如“ѭ8”之类的字段,很难说不知道您的模型是什么样子。但主要思想是将该字段定向到该特定用户的注释并使用
.count()
。如果您提供更多代码,我可以更新我的答案(否则其他人会更好)。 更新:对不起,忘记了文档。对于
.count()
:http://docs.djangoproject.com/en/dev/ref/models/querysets/#count 过滤器:http://docs.djangoproject.com/en/dev/ref/models/querysets/#filter 用于注册管理员:http://docs.djangoproject.com/en/dev/ref/contrib/admin/     

要回复问题请先登录注册