django admin 里面只读禁止写入操作的方法
 

from django.contrib import admin
from .models import *

from import_export import resources
from import_export.admin import  ImportExportActionModelAdmin


# 主机资产
@admin.register(Asset)
class Assetadmin(ImportExportActionModelAdmin):
    resource_class = ProxyResource  # import_export支持导入导出

    # 禁用编辑链接的只是不让它在页面中显示;进一步可以修改readonly_fieldss和exclude以及重写save方法
    def has_change_permission(self, request, obj=None):
        """隐藏保存按钮"""
        return False

    def has_add_permission(self, request, obj=None):
        """隐藏添加按钮"""
        return False

    def has_delete_permission(self, request, obj=None):
        """隐藏删除按钮"""
        return False

    def get_actions(self, request):
        # 在actions中去掉‘删除'操作
        actions = super(Assetadmin, self).get_actions(request)
        if request.user.username[0].upper() != 'J':
            if 'delete_selected' in actions:
                del actions['delete_selected']
        return actions

    def save_model(self, request, obj, form, change):
        """禁用save数据写入"""
        return False

    all_fields = list(key.name for key in Asset._meta.fields)
    readonly_fieldss = all_fields  # 排除字段
    exclude = all_fields  # 排除字段
    List_display_links = None  # 禁用编辑链接


    list_display = all_fields # 显示所有字段

文章作者: 以谁为师
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源!
              
  目录