2

I am aware of the following questions which are pretty different:

My question is a little different: how can I disable the action button in the model list view, but retain the add functionality and links for all other Django parts (for example OneToOne relations and inlines). The code:

@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "template", "file")
    fields = ["template", "name", "file"]

    def has_add_permission(self, request):
        return False

disables completely the add functionality of ModelAdmin (Django 3.2+, not tested in early versions).

J_Zar
  • 2,034
  • 2
  • 21
  • 34

1 Answers1

3

A possibility is:

@admin.register(Document)
class DocumentAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "template", "file")
    fields = ["template", "name", "file"]

    def has_add_permission(self, request):
        return ("add" in request.path or "change" in request.path)

This will allow to maintain the "/admin/<app>/<model>/add/" functionality, also in popup. The model list view will allow the model edit but it will not have the "add" button.

J_Zar
  • 2,034
  • 2
  • 21
  • 34