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.
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?
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:
This is how the table is displayed now:
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-
I want to hide the edit, add, and remove icons from the Django admin tool for a foreign key field.
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?
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:
This is how the table is displayed now:
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-