Python Django Admin - 禁用特定模型的“添加”操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4143886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:26:27  来源:igfitidea点击:

Django Admin - Disable the 'Add' action for a specific model

pythondjangodjango-admin

提问by Rory

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.

我有一个 django 站点,里面有很多模型和表单。我有许多自定义表单和表单集以及内联表单集以及自定义验证和自定义查询集。因此,添加模型操作取决于需要其他东西的表单,并且 django 管理中的“添加模型”通过自定义查询集中的 500。

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

无论如何要禁用某些模型的“添加 $MODEL”功能?

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/modelnameview.

我想/admin/appname/modelname/add/给出 404(或合适的“离开”错误消息),我不希望显示“添加 $MODELNAME”按钮/admin/appname/modelname

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?

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

采纳答案by Frost.baka

It is easy, just overload has_add_permissionmethod in your Adminclass like so:

这很简单,只需has_add_permission在您的Admin类中重载方法,如下所示:

class MyAdmin(admin.ModelAdmin):
     def has_add_permission(self, request, obj=None):
        return False

回答by Richard Cooke

By default syncdb creates 3 security permissions for each model:

默认情况下,syncdb 为每个模型创建 3 个安全权限:

  1. Create (aka add)
  2. Change
  3. Delete
  1. 创建(又名添加)
  2. 改变
  3. 删除

If your logged in as Admin, you get EVERYTHINGno matter what.

如果您以管理员身份登录,则无论如何都可以获得一切

But if you create a new user groupcalled "General Access"(for example)then you can assign ONLY the CHANGE and DELETE permissions for all of your models.

但是,如果您创建一个名为“General Access”的新用户组(例如),那么您只能为所有模型分配 CHANGE 和 DELETE 权限。

Then any logged in user that is a member of that group will not have "Create" permission, nothing related to it will show on the screen.

那么作为该组成员的任何登录用户都将没有“创建”权限,与它相关的任何内容都不会显示在屏幕上。

回答by Mohammad

This is a too much delayed answer; Just posting this as if anyone is finding the same solution.

这是一个太延迟的答案;只是张贴这个就好像有人找到了相同的解决方案。

In admin.py file you can do the following:

在 admin.py 文件中,您可以执行以下操作:

class MyModelForm(forms.ModelForm):

class Meta:
    model = MyModel
    fields = '__all__'


class MyModelAdmin(admin.ModelAdmin):
    form = QuestionTrackAdminForm
    list_display = ['title', 'weight']
    readonly_fields = ['title', 'weight']

admin.site.register(MyModel, MyModelAdmin)

Here, "readonly_fields" does the magic. Thanks.

在这里,“readonly_fields”发挥了作用。谢谢。

回答by Sandeep Prasad Kushwaha

I think this will help you.. below code must be in admin.py file

我认为这会对你有所帮助..下面的代码必须在 admin.py 文件中

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    list_display = ('name', )
    list_filter = ('name', )
    search_fields = ('name', )
    list_per_page = 20

    # This will help you to disbale add functionality
    def has_add_permission(self, request):
        return False

    # This will help you to disable delete functionaliyt
    def has_delete_permission(self, request, obj=None):
        return False

In additon to the above as posted by

除了以上发布的

    # This will help you to disable change functionality
    def has_change_permission(self, request, obj=None):
        return False

回答by C.K.

Just copy code from another answer

只需从另一个答案复制代码

# In admin
# make the related field can't be added
    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        form.base_fields['service'].widget.can_add_related = False
        return form

In my case I use inline

在我的情况下,我使用内联

# In inline formset e.g. admin.TabularInline
# disable all
    def get_formset(self, request, obj=None, **kwargs):
        formset = super().get_formset(request, obj, **kwargs)
        service = formset.form.base_fields['service']
        service.widget.can_add_related = service.widget.can_change_related = service.widget.can_delete_related = False
        return formset

in service = formset.form.base_fields['service']base_fieldsis the fields defined in model

inservice = formset.form.base_fields['service']base_fields是模型中定义的字段

if defined in the form use:

如果在表单中定义,请使用:

product = formset.form.declared_fields['product']

product = formset.form.declared_fields['product']

see also

也可以看看