2021-10-30

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



from Recent Questions - Stack Overflow https://ift.tt/3CubyCO
https://ift.tt/3nYmFOr

No comments:

Post a Comment