【Django Admin】admin后台自定义返回显示属性

# admin.py 返回图片


# Admin自定义返回列表PRO  例子:照片邮箱
def head_picture(self,models_obj):
    return format_html('<img src="{}" height="50" width="50">', '{}'.format(models_obj.head))
head_picture.short_description = '照片'

 

 

 

 

    # Admin自定义返回列表PRO  例子:成绩
    def score_custom(self,models_obj):
        if models_obj.score < 60:
            font_color = "red"
        else:
            font_color = "green"
        return format_html('<span style="color:{};">{}</span>'.format(font_color,models_obj.score))
score_custom.admin_order_field = 'score' # 继承admin原字段的排序 score_custom.short_description
= '成绩'

 

posted @ 2022-06-29 12:43  PythonNew_Mr.Wang  Views(194)  Comments(0)    收藏  举报