Django admin 的 list_display 变量统计后台作者发贴总数

models.py

class Article(models.Model):
    '''
    文章表
    '''
    title = models.CharField(u"文章标题",max_length=255,unique=True)
    categroy = models.ForeignKey("Category",verbose_name=u"板块")
    #blank=True,null=True,后台提交form允许keywords为空
    keywords = models.CharField(u'文章关键字',max_length=255,blank=True,null=True)
    description = models.TextField(u'描述',max_length=200,blank=True,null=True)
    head_img = models.ImageField(u"缩略图",upload_to="static/uploads")
    #content = models.TextField(u"文章内容",)
    content = RichTextField(blank=True,null=True,verbose_name="文章内容")
    author = models.ForeignKey("UserProfile",verbose_name=u"作者")
    publish_date = models.DateTimeField(u'发布时间',auto_now=True)
    hideden = models.BooleanField(u"是否隐藏",default=False)
    weight = models.IntegerField(u"优先级",default=1000)

    def __unicode__(self):
        return "<%s,author:%s>" %(self.title,self.author)



class UserProfile(models.Model):
    '''
    用户表
    '''

    user = models.OneToOneField(User)
    name = models.CharField(max_length=32)
    group = models.ManyToManyField('UserGroup')

    def __unicode__(self):
        return self.name


 
  admin.py 
  

#自定义userProfile列表项
class UserprofileAdmin(admin.ModelAdmin):
    list_display = ('name','get_user_article')

    def get_user_article(self,user_id):
        num = models.Article.objects.filter(author_id=user_id)
        return len(num)

    get_user_article.short_description = u'发贴数'



admin.site.register(models.UserProfile,UserprofileAdmin)


后台userprofile 列表展示结果


你可能感兴趣的:(Django admin 的 list_display 变量统计后台作者发贴总数)