[英]Django admin - remove "add another" button for a self-referential field
I have a Django project that includes a model class with an optional self-referential ForeignKey field.我有一个 Django 项目,其中包含一个带有可选的自引用 ForeignKey 字段的模型类。 A partial snippet:
部分片段:
class Site(models.model):
name = models.CharField(max_length=100)
parent_site = models.ForeignKey('self', null=True, blank=True)
I'm using the Django admin site to create new objects.我正在使用 Django 管理站点来创建新对象。 For this class' admin form I'd like to disable the " Add another... " button next to the
parent_site
field (ie when you're creating a new site, you can't open the popup to create another new site as the parent).对于此类的管理表单,我想禁用
parent_site
字段旁边的“添加另一个... ”按钮(即,当您创建新站点时,您无法打开弹出窗口来创建另一个新站点)父母)。
I can't remove has_add_permission
from the user, as they need it to be in the current add view.我无法从用户中删除
has_add_permission
,因为他们需要它在当前添加视图中。 I don't mind removing the function from both add and change views, but limiting removal to the add view would be helpful.我不介意从添加和更改视图中删除该功能,但限制删除添加视图会有所帮助。
I haven't been able to work out how to use the Inline field classes to achieve this, or formfield_for_foreignkey
, or a custom ModelForm
.我一直无法弄清楚如何使用内联字段类来实现这一点,或
formfield_for_foreignkey
或自定义ModelForm
。 Anyone got a solution more elegant than using JavaScript on a customised form template?有人有比在自定义表单模板上使用 JavaScript 更优雅的解决方案吗?
no css hacks add to admin class:没有 css hacks 添加到管理类:
max_num=0
or try this in admin.py ( for older django versions):或在 admin.py 中尝试此操作(对于较旧的 Django 版本):
class MODEL_ADMIN(admin.ModelAdmin):
class Media:
css = {'all': ('css/no-addanother-button.css',)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.