python web全栈,  python全栈,  后端开发,  系统运维

自定义django admins.py 展示字段

from django.contrib import admin

# Register your models here.
from django.contrib import admin
import json
from import_export.admin import ImportExportModelAdmin
# Register your models here.
from .models import Needs, Projects, NeedPool
from django.utils.safestring import mark_safe

class NeedPoolAdmin(ImportExportModelAdmin):
    list_display = ('need_type', 'need_priority', 'remark', 'name', 'department', 'business_id', 'file_url')
    search_fields = ('need_type', 'need_priority', 'remark', 'name', 'department', 'business_id')
    list_filter = ('need_type', 'need_priority', 'remark', 'name', 'department', )

    def file_url(self, obj):
        path_file = obj.patch_file
        html = '<html> <a href="/static/%s">%s</a> </html>' % (path_file, path_file)
        return mark_safe(html)  # 取消转义

    file_url.short_description = "附件"
    file_url.allow_tags = True

    # 重写编辑页, 继承父类方法
    def change_view(self, request, object_id, extra_context=None):
        self.fields = ("name", "created", "content", "image_url")  # 将自定义的字段注册到编辑页中
        self.readonly_fields = ("name", "created", "image_url")  # 务必将该字段设置为仅限可读, 否则抛出异常
        return super(NeedPoolAdmin, self).change_view(request, object_id, extra_context=extra_context)

admin.site.register(NeedPool, NeedPoolAdmin)

file

留言

您的电子邮箱地址不会被公开。 必填项已用*标注

闽ICP备20008591号-1