在管理站点中创建隐藏字段
如何在管理站点中创建完全隐藏的字段(输入和标签)? 我知道 exclude
属性,但它完全从模板中排除该字段,而我在网页中需要它,但隐藏:
class OutForm(ModelForm):
reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), widget=forms.HiddenInput)
在管理模板中,我实际上可以隐藏一个字段,但不能隐藏它标签。
How can I create a fully hidden field (input and label) in the admin site?
I know about the exclude
property, but it fully excludes the field from the template, while I need it in the web page, but hidden:
class OutForm(ModelForm):
reply_to = forms.ModelChoiceField(queryset=InMessages.objects.all(), widget=forms.HiddenInput)
In the admin template I actually can hide a field, but not its label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Django 管理员尚不支持隐藏字段。有一个开放的票证: https://code.djangoproject.com/ticket/11277
但是,有一些解决方法不需要 jQuery。管理表单使用
admin/includes/fieldset.html
呈现。如果您覆盖此模板,则可以注入一个 CSS 类来表示要隐藏的行:这实际上是文件中的一行,我已对其进行了扩展以使其更具可读性。
(整洁的细节:对于 StackedInline/TabularInline 对象,您可以在 Python 代码中将模板指定为变量)
接下来,您可以在 CSS 中隐藏该行:
您可以通过管理页面加载:
或使用 modeladmin 中的媒体定义:
The Django admin does not support hidden fields yet. There is an open ticket for that: https://code.djangoproject.com/ticket/11277
However, there are workarounds that don't require jQuery. The admin forms are rendered using
admin/includes/fieldset.html
. If you override this template, you can inject a CSS class to denote the row for hiding:this is actually a single line in the file, I've expanded it to make it more readable.
( Neat detail: for an StackedInline/TabularInline objects, you can specify the template as variable in Python code. )
Next, you can hide that row in your CSS:
Which you can load via your admin page:
or by using the media definition in the modeladmin:
在模板中,显示时
会隐藏隐藏字段标签。
In the template, while displaying
It will hide the hidden field labels.
我发现在字段集中使用“classes”属性来隐藏字段非常方便,但仍将它们保留在请求中。在你的模型管理中你可以写
I found it quite handy to use the 'classes' attribute in fieldsets to hide fields but still leave them in a request. In your model admin you can write
您在示例中给出的是 ModelForm,而不是您应该用于管理站点的 ModelAdmin。
无论如何,排除某些字段的方法是相同的:在
exclude
属性中指定它:或
参阅 Django 文档了解详细信息:http://docs.djangoproject.com/en/1.2/ref/contrib/admin/
You're giving ModelForm in your example, not ModelAdmin you should be using for admin site.
Anyway, method of excluding some field is the same: specify it in
exclude
property:or
See Django documentation for details: http://docs.djangoproject.com/en/1.2/ref/contrib/admin/
尝试将 label="" 添加到 ModelChoiceField 以使标签成为空字符串:
Try adding label="" to your ModelChoiceField to make the label an empty string:
看起来 Django 3.2 中的 HiddenInput 小部件足以隐藏表单中的整行。我已经通过自定义管理表单和 formfield_override 属性尝试过此操作。包含隐藏字段的类“form-row”的 div 获得附加类“hidden”,并且不显示。
It seems that in Django 3.2 the HiddenInput widget is enough to hide the entire row from the form. I have tried this both via custom admin form and via the formfield_override property. The div with class "form-row" containing the hidden field gets additional class "hidden" and is not shown.