后台信息列表中添加字段以外的项目、按钮

自定义操作按钮

应用下adminx.py 部分代码

....
from django.utils.safestring import mark_safe 

    # 自定义操作按钮
    def buttons(self, obj):
        button_html = "编辑"%obj.id + " 删除"%obj.id
        return mark_safe(button_html)
    buttons.short_description = "操作"
    # 添加到list_display
    list_display = ['id','title','category','modified_time','views','status','over_time','buttons']
...
字段外的内容项

models.py部分代码

...
class Post(models.Model):
    """文章"""
    status_choices = (
        (0, '未通过'),
        (1, '已通过'),
        (2, '草稿')
    )
    title = models.CharField(max_length=100,verbose_name=u'标题')
    body = UEditorField(verbose_name=u'内容',width=600, height=300,toolbars="full",imagePath="advices/ueditor/%(datetime)s.%(extname)s",filePath="advices/ueditor/%(datetime)s.%(extname)s", default='')
    created_time = models.DateTimeField(verbose_name=u'录入时间',auto_now_add=True)     #不出现在后台
    modified_time = models.DateTimeField(verbose_name=u'更新时间',default = timezone.now)        #不出现在后台
    modified_time.editable = True       #出现在后台
    excerpt = models.CharField(max_length=200, blank=True,verbose_name=u'简介')  # 文章摘要,可为空
    category = models.ForeignKey('Category',to_field='id',on_delete='models.CASCADE',verbose_name=u'栏目',default='')  # ForeignKey表示1对多(多个post对应1个category)
    tags = models.ManyToManyField(Tag, blank=True,verbose_name=u'标签')
    views = models.PositiveIntegerField(default=0,verbose_name=u'点击数')  # 阅读量
    status = models.SmallIntegerField(default=2,choices=status_choices,verbose_name=u'状态')  # 状态 

    def over_time(self):
        if self.created_time:
            now = timezone.now()
            month = now.month - self.created_time.month
            day = now.day - self.created_time.day
            if not month:
                text = "

已发表{}天

".format(day) else: text = "

已发表{}月{}天

".format(month, day) return mark_safe(text) else: return mark_safe("") over_time.short_description = '发表时间' class Meta: # 设置model在后台管理系统中的中文名称 verbose_name = u'博客' verbose_name_plural = verbose_name unique_together = ('title', 'category') def __str__(self): return self.title ...

你可能感兴趣的:(后台信息列表中添加字段以外的项目、按钮)