gpt4 book ai didi

python - 在 list_display 中的字段时隐藏 Django 管理表中的编辑/添加/删除按钮

转载 作者:行者123 更新时间:2023-12-05 03:54:23 25 4
gpt4 key购买 nike

我想在 Django 管理工具中隐藏外键字段的编辑、添加和删除图标。

enter image description here

这有可能实现吗?如果是,怎么办?

到目前为止,这是我的代码:

@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

看来问题是我还注册了另一个模型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"]

但是,我也需要保留这个模型。那么,我该如何实现呢?

最佳答案

通过在内部 class Media 中指定文件,我可以通过为 RequestAdmin 使用自定义 CSS 来隐藏这些图标。 :

@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",)}

volunteering/static/volunteering/css/style.css 的内容如下:

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

应用的结构是这样的:

enter image description here

表格现在是这样显示的:

enter image description here

关于python - 在 list_display 中的字段时隐藏 Django 管理表中的编辑/添加/删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61050771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com