PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 19, 2022

[FIXED] How can I hide (not disable) in Django admin the action add model button in ModelAdmin list view?

 October 19, 2022     admin, django, django-admin, python     No comments   

Issue

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

  • Django Admin - Disable the 'Add' action for a specific model
  • Disable link to edit object in django's admin (display list only)?

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


Solution

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.



Answered By - J_Zar
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing