How to Disable some action and filed on Model Form on site admin


  1. I want to disable delete and add an action for this model
    so this model can be only updated through this form on the admin site ( disable 1 and 2 in the picture)

2.Not allow to input data in Source Name Field ( 3 in the picture)

Please recommend any solution or link document to study how to customize the form in admin site like above.

Thank you for any help

For #1 and #2 you’ll need to override has_add_permission and has_delete_permission to check if the user has permission to do those actions. If not, the admin will not show the + and the delete button.

SO example

For #3 you can use ModelAdmin.get_form method to specify the form to use admin.

Hi, thank you for a quick reply.
I try to do as your recommendation , it is ok for my three requirements.
it is great but requirement#3 I just apply readonly_fields ( not use you can use ModelAdmin.get_form) I don’t know how to do in the next step.

I have one additional question
Type name field can be enabled in Add- Mode but read only in Change-Mode
How can I do?

Once I did, this field disappears. as I understand because it is set as read_only

class CostTypeAdmin(admin.ModelAdmin):
    actions =None
    readonly_fields = ('type_name',)
    search_fields = ['type_name']
    # def has_add_permission(self, request, obj=None):
    #     return False
    def has_delete_permission(self, request, obj=None):
        return False

If I understand what you’re trying to do you could use get_readonly_fields. (Source) (Django docs)

def get_readonly_fields(self, request, obj=None):
    if obj:
        # On update type_name will be read-only
        return ["type_name"]
        # On creation all fields will be editable
        return []
1 Like

Thank you for your great support.
You help me save time to customize Django admin very much much

The Django Admin Cookbook is very helpful for me to study more apart from The Django admin site

I got things done because of you.

1 Like