How to disable add permission in the Django's admin page when the choices drop down field doesn't contain any data?

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 是一个空元组时,这工作正常,然后我想禁用管理页面上的任何添加权限。

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

当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():通过调用 RadisCache class.

is_redis_available 函数进行 Redis 检查

为什么不能在权限方法中直接加载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