admin 自定义字段颜色 并加以简单判断-LMLPHP
在model中

class Books(models.Model):
nid = models.AutoField(primary_key=True, )
title = models.CharField(max_length=32, verbose_name="书名")
date = models.DateField(verbose_name="出版日期")
price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="价格")
publish = models.ForeignKey(to=Publishes, on_delete=models.CASCADE, verbose_name="出版社")
author = models.ManyToManyField(to="Authors") def 价格(self):
if self.price > 500:
return mark_safe(('<span style="color: red;">{}</span>').format(self.price))
return mark_safe(('<span style="color: green;">{}</span>').format(self.price)) def __str__(self):
return self.title class Meta:
verbose_name_plural = "书籍" admin中
list_display = ["nid", "title", "price", "publish", "date", "作者", "价格"]
不过不能编辑

05-07 15:45