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

Question:

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?

Asked By: lmiguelvargasf

||

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

Answered By: lmiguelvargasf

I am curious about how did you make that combobox or the list box from volunteers, I am trying to make it but to show items from a database as a list box, but I don’t know how to do it, thanks-

Answered By: davidconejo97
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.