Django Admin - 禁用特定模型的“添加”操作 [英] Django Admin - Disable the 'Add' action for a specific model

查看:189
本文介绍了Django Admin - 禁用特定模型的“添加”操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django网站有很多的模型和形式。我有很多自定义窗体和窗体,以及内置窗体和自定义验证和自定义查询。因此,添加模型操作取决于需要其他内容的表单,并且django管理员中的添加模型通过自定义查询器中的500。



有没有禁用某些型号的添加$ MODEL功能?



我想要 / admin / appname / modelname / add / 给出404(或适当的离开错误消息),我不想要在$ code/ admin / appname / modelname上添加$ MODELNAME按钮视图。



Django管理员提供了一种方法来禁用管理员操作(http://docs.djangoproject.com/en/dev/ref/contrib/admin/actions/ #disabling-actions),但此模型的唯一操作是delete_selected。即管理员操作只对现有型号起作用。有没有一些django-esque的方式来做到这一点?

解决方案

很容易,只要在你的Admin类中重载has_add_permission方法, :

$ b

  class MyAdmin(admin.ModelAdmin):
def has_add_permission(self,request):
return假


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.

Is there anyway to disable the 'Add $MODEL' functionality for a certain models?

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 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?

解决方案

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):
        return False

这篇关于Django Admin - 禁用特定模型的“添加”操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆