首页 > 解决方案 > How to disable add permission in the Django's admin page when the choices drop down field doesn't contain any data?

问题描述

我有一个模型,它有一个 template_name 字段,在 django admin 中显示为下拉菜单。

template_id = models.CharField(max_length=255, choices=template_data, unique=True)

template_data正在从其他一些脚本中填充。template_data 的格式为:(('123', 'car'), ('456', 'bike'))

template_data当有一些值但是当它是一个空元组时,这很好template_data用,那么我想禁用管理页面上的任何添加权限。

template_data目前当redis关闭时,添加按钮被禁用但是当它是一个空元组时如何做同样的事情?

Redis关闭时禁用添加权限:

def has_add_permission(self, request, obj=None):
    is_redis = RedisCache.is_redis_available()
    if is_redis is not True:
        return False
    return True

RedisCache.is_redis_available(): Redis 检查是通过调用类的is_redis_availablefunc 来进行的RadisCache

标签: pythondjangodjango-modelsdjango-rest-frameworkdjango-admin

解决方案


为什么不能直接在权限方法中加载template_data?

def has_add_permission(self, request, obj=None):
    if not template_data:
        return False
    is_redis = RedisCache.is_redis_available()
    if is_redis is not True:
        return False
    return True

推荐阅读