如果表达式为True,请更改django admin界面中字段的字体/颜色 [英] Change font/color for a field in django admin interface if expression is True
问题描述
在django admin界面中的更改列表视图中,是否可以在某些字段/行红色中标记表达式?
In change list view in django admin interface, is it possible to mark some fields/rows red in if they achieve a expression?
例如,如果有组与
成员
和容量
的模型 或拥挤?
For example, if there is a model Group
with members
and capacity
, how can I visualize when they are full or crowded?
推荐答案
和更改列表视图中显示的内容,可以使用 list_display
选项 ModelAdmin
。
For modifying how and what is displayed in change list view, one can use list_display
option of ModelAdmin
.
请注意, list_display
不是真正的数据库字段不能用于排序,所以需要给Django管理员一个关于实际用于排序的数据库字段的提示。
Mind you, columns given in list_display
that are not real database fields can not be used for sorting, so one needs to give Django admin a hint about which database field to actually use for sorting.
一个doe这是通过将 admin_order_field
属性设置为用于在HTML中包装某些值的可调用的。
One does this by setting admin_order_field
attribute to the callable used to wrap some value in HTML for example.
Django的示例多彩字段文档:
Example from Django docs for colorful fields:
class Person(models.Model):
first_name = models.CharField(max_length=50)
color_code = models.CharField(max_length=6)
def colored_first_name(self):
return '<span style="color: #%s;">%s</span>' % (
self.color_code, self.first_name)
colored_first_name.allow_tags = True
colored_first_name.admin_order_field = 'first_name'
class PersonAdmin(admin.ModelAdmin):
list_display = ('first_name', 'colored_first_name')
我希望有些有所帮助。
这篇关于如果表达式为True,请更改django admin界面中字段的字体/颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!