Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide edit/add/remove buttons in Django admin table when field in list_display

I want to hide the edit, add, and remove icons from the Django admin tool for a foreign key field.

enter image description here

Is it possible to achieve this? If so, how?

This is my code so far:

@admin.register(Request)
class RequestAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "contact_method",
        "neighborhood",
        "adults",
        "children",
        "prescriptions",
        "volunteer",
        "status",
        "due_date",
    )
    list_editable = ("status", "volunteer")

    def neighborhood(self, obj):
        if obj.address and obj.address.get("neighborhood", False):
            neighborhood = obj.address["neighborhood"]
            if obj.address.get("details", False):
                return f"{neighborhood} - {obj.address['details']}"
            return neighborhood

It seems the problem is that I have also registered another model Volunteer.

@admin.register(Volunteer)
class VolunteerAdmin(admin.ModelAdmin):
    list_display = ("name", "contact_method", "neighborhood", "valid_ID")

    def neighborhood(self, obj):
        if obj.address and obj.address.get("neighborhood", False):
            return obj.address["neighborhood"]

However, I need to keep this model too. So, how can I achieve this?

like image 665
lmiguelvargasf Avatar asked Sep 07 '25 21:09

lmiguelvargasf


1 Answers

I was able to hide this icons by using custom CSS for RequestAdmin by specifying the file in an internal class Media:

@admin.register(Request)
class RequestAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "contact_method",
        "neighborhood",
        "adults",
        "children",
        "prescriptions",
        "volunteer",
        "status",
        "due_date",
    )
    list_editable = ("status", "volunteer")

    def neighborhood(self, obj):
        if obj.address and obj.address.get("neighborhood", False):
            neighborhood = obj.address["neighborhood"]
            if obj.address.get("details", False):
                return f"{neighborhood} - {obj.address['details']}"
            return neighborhood

    # This is required to use custom extra CSS
    class Media:
        css = {"all": ("volunteering/css/style.css",)}

The content of volunteering/static/volunteering/css/style.css is the following:

.related-widget-wrapper-link {
    display: none;
}

This is how the app is structured:

enter image description here

This is how the table is displayed now:

enter image description here

like image 151
lmiguelvargasf Avatar answered Sep 11 '25 08:09

lmiguelvargasf