gpt4 book ai didi

django - 在 django admin 中隐藏 "add", "change", "delete" Action

转载 作者:行者123 更新时间:2023-12-05 06:24:16 25 4
gpt4 key购买 nike

我有一段代码是这样的:

class PlatformEnvInLine(admin.TabularInline):
model = PlatformEnv
extra = 1
classes = ['collapse']
fields = ('environment',)

我的 PlatformEnv 模型如下所示:

class PlatformEnv(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
...
environment = models.ForeignKey(Environment, models.DO_NOTHING, db_column='Environment_ID', blank=True, null=True)

现在显示 PlatformEnvInLin 时,您可以通过下拉菜单选择一个环境。但是在下拉菜单旁边显示了用于添加、更改或删除环境的按钮。如何隐藏这些按钮?

最佳答案

您应该能够通过覆盖 formfield_for_dbfield 方法来做到这一点。

class PlatformEnvInLine(admin.TabularInline):
model = PlatformEnv
extra = 1
classes = ['collapse']
fields = ('environment',)

def formfield_for_dbfield(self, db_field, request, **kwargs):
formfield = super().formfield_for_dbfield(db_field, request, **kwargs)
if db_field.name == 'environment':
formfield.widget.can_add_related = False
formfield.widget.can_change_related = False
formfield.widget.can_delete_related = False
return formfield

关于django - 在 django admin 中隐藏 "add", "change", "delete" Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789345/

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