Amine Messaoudi
Amine Messaoudi

Reputation: 2279

How can I disable deletion of inline objects when using django-reverse-admin?

This is my ModelAdmin:

class ComputerAdmin(ReverseModelAdmin):
    list_display = ('employee', 'ip', 'mac', 'name', 'hardware')
    list_filter = ('employee__branch', )
    inline_type = 'tabular'
    inline_reverse = ['hardware', ]
    show_full_result_count = False

This is how it shows when adding a new computer:

Enter image description here

As you can see, I don't want to have the delete column and delete icon, because I have a foreign key so only one element is allowed. How can I do that?

Does django-reverse-admin have anything like has_delete_permisison for inlines only and not the whole ModelAdmin? I have already searched in documentation with no results, which is why I am posting here.

I updated my code as below:

class HardwareInline(admin.TabularInline):

    model = Hardware

    def has_delete_permission(self, request, obj=None):
        return False

class EmployeeAdmin(admin.ModelAdmin):

    list_display = ('group', 'branch', 'name')
    list_filter = ('group', )

class ComputerAdmin(ReverseModelAdmin):

    list_display = ('employee', 'ip', 'mac', 'name', 'hardware')
    list_filter = ('employee__group', 'employee__branch', )
    inline_type = 'tabular'
    inline_reverse = [ { 'field_name': 'hardware', 'admin_class': HardwareInline } ]

The delete column disappeared from inline, but I get a delete button under it:

Enter image description here

Upvotes: 0

Views: 864

Answers (1)

ruohola
ruohola

Reputation: 24038

We can find the answer from this GitHub issue:

You need to explicitly create an inline, and add a has_delete_permission method to it:

class HardwareInline(admin.TabularInline):
    model = Hardware

    def has_delete_permission(self, request, obj=None):
        return False

And then in your ReverseModelAdmin, you need to specify the 'admin_class':

class ComputerAdmin(ReverseModelAdmin):
    list_display = ('employee', 'ip', 'mac', 'name', 'hardware')
    list_filter = ('employee__group', 'employee__branch')
    inline_type = 'tabular'
    inline_reverse = [
        {'field_name': 'hardware', 'admin_class': HardwareInline},
    ]

This should hide the "Delete" checkbox.

Upvotes: 2

Related Questions