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

我有一个包含很多模型和表格的django网站。我有许多自定义表单和表单集以及inlineformsets和自定义验证和自定义查询集。因此,添加模型操作取决于需要其他内容的表单,并且Django管理员中的“添加模型”通过自定义查询集中的500进行操作。

无论如何,对于某些型号,是否禁用“添加$ MODEL”功能?

我想/admin/appname/modelname/add/给出一个404(或适当的“ goaway”错误消息),我不希望显示“ Add $ MODELNAME”按钮/admin/appname/modelname

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

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?


回答 0

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

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

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

回答 1

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

  1. 创建(又名添加)
  2. 更改
  3. 删除

如果您以Admin身份登录,则无论如何,您将获得一切

但是,如果您创建一个名为“常规访问”的新用户组(例如),则只能为所有模型分配“更改”和“删除”权限。

然后,任何属于该组成员的登录用户将没有“创建”权限,与此相关的任何内容都不会显示在屏幕上。

By default syncdb creates 3 security permissions for each model:

  1. Create (aka add)
  2. Change
  3. Delete

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

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

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.


回答 2

我认为这将对您有所帮助..下面的代码必须在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

除上述内容外,发布者

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

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

@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

回答 3

只需复制另一个答案中的代码

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

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

如果以以下形式定义:

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

也可以看看

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_fields is the fields defined in model

if defined in the form use:

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

see also


回答 4

这是一个过于延迟的答案;只需将其发布,就好像有人在寻找相同的解决方案一样。

在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)

在这里,“ readonly_fields”起到了神奇的作用。谢谢。

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:

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.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。