使用django admin 显示字段自定义def的时候 默认会显示成函数名称,使用一下方法可自定义显示名称
第一种
class TemplateRequestLog(models.Model): templateApp = models.ForeignKey(TemplateApp, verbose_name="模板名称", on_delete=models.CASCADE) requestUrl = models.URLField(verbose_name="请求路径") createTime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间") lastTime = models.DateTimeField(auto_now=True, verbose_name="修改时间") editor = models.ForeignKey(User, null=True, on_delete=models.CASCADE, verbose_name="修改者") class Meta: verbose_name_plural = "访问记录" def requestName(self): return self.editor.first_name requestName.short_description = "微信昵称" # 直接修改
第二种
def rename(newname): def decorator(fn): fn.__name__ = newname return fn return decorator class AppNews(models.Model): topImage = models.ImageField(upload_to=‘../static/uploadImage/AppNews/%Y/%m/%d‘, verbose_name=‘滚动图片‘, null=True, blank=True) createTime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间") lastTime = models.DateTimeField(auto_now=True, verbose_name="修改时间") editor = models.ForeignKey(User, null=True, on_delete=models.CASCADE, verbose_name="修改者") class Meta: verbose_name_plural = "滚动新闻" @rename("模板头像") # 添加在顶部 def showTopImage(self): return format_html("<img src=‘/{}‘ class=‘showAvatar‘/>", self.topImage) def __str__(self): return self.describe
原文地址:https://www.cnblogs.com/wangcongxing/p/11683380.html
时间: 2024-10-07 16:52:27