python - 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_available
func 来进行的RadisCache
。
解决方案
为什么不能直接在权限方法中加载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
推荐阅读
- angular - 为什么 Google Lighthouse 会为 Angular redirectTo 路由返回 404?
- python - 如何在熊猫中使用特定条件进行分组?
- javascript - 带有 TypeScript 和 Jest 的 Github CI
- flutter - 如果列表为空,ListView 不显示结果
- c - 指针变量无法正确处理内存地址
- c++ - 为使用 gRPC 安装的 Protobuf 配置 VsCode 包含路径
- css - 悬停时如何使带有链接的图像变淡
- css - 为什么我们使用响应式方法,而 % 属性使我们的页面适应任何屏幕尺寸?
- arduino - LED 不亮
- android - 在 Android 中设置 FLAG_LAYOUT_IN_SCREEN 后禁用全屏模式