Django Admin - Disable the #39;Add#39; action for a specific model(Django Admin - 禁用特定模型的“添加操作)
问题描述
我有一个 django 网站,里面有很多模型和表格.我有许多自定义表单和表单集以及内联表单集以及自定义验证和自定义查询集.因此,添加模型操作取决于需要其他东西的表单,并且 django 管理员中的添加模型"通过自定义查询集中的 500.
I have a django site with lots of models and forms. I have many custom forms and formsets and inlineformsets and custom validation and custom querysets. Hence the add model action depends on forms that need other things, and the 'add model' in the django admin throughs a 500 from a custom queryset.
是否可以禁用某些型号的添加 $MODEL"功能?
Is there anyway to disable the 'Add $MODEL' functionality for a certain models?
我希望 /admin/appname/modelname/add/
给出 404(或合适的离开"错误消息),我不希望出现添加 $MODELNAME"按钮在 /admin/appname/modelname
视图上.
I want /admin/appname/modelname/add/
to give a 404 (or suitable 'go away' error message), I don't want the 'Add $MODELNAME' button to be on /admin/appname/modelname
view.
Django admin 提供了一种禁用管理操作的方法 (http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-actions) 但是此模型的唯一操作是 'delete_selected'.即管理员操作仅对现有模型起作用.有没有一些 django 风格的方法来做到这一点?
Django admin provides a way to disable admin actions (http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/#disabling-actions) however the only action for this model is 'delete_selected'. i.e. the admin actions only act on existing models. Is there some django-esque way to do this?
推荐答案
很简单,只需像这样在 Admin
类中重载 has_add_permission
方法:
It is easy, just overload has_add_permission
method in your Admin
class like so:
class MyAdmin(admin.ModelAdmin):
def has_add_permission(self, request, obj=None):
return False
这篇关于Django Admin - 禁用特定模型的“添加"操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django Admin - 禁用特定模型的“添加"操作


基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01