Django admin在同一行显示多个字段
我创建了一个模型,它将自动显示模型中的所有字段并将其显示在管理页面上。
现在,我有一个问题,我想在同一行上有两个字段,为此我必须在 ModelAdmin 中指定字段集:
fieldsets = (
(None, {
'fields': (('firstname', 'lastname'),)
}),
)
我必须指定所有字段吗? 因为有我需要指定数据库中的许多字段。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有一篇文章可能有用
http:// /amk1.wordpress.com/2010/09/23/a-2-column-django-admin-form/
文章引用如下:
There is an article may be useful
http://amk1.wordpress.com/2010/09/23/a-2-column-django-admin-form/
Article is quote below:
恐怕没有一个简单的方法可以做到这一点。
一种选择是覆盖该 ModelAdmin 的change_form.html 模板并根据您的喜好设置表单样式。
另一种替代方法是执行自定义 ModelForm 并使用呈现两个输入字段的小部件定义一个字段,在表单的 .save() 方法中,将小部件结果值(元组)设置为这两个字段。
I'm afraid there's not an easy way to do it.
One option is to override the change_form.html template for that ModelAdmin and style the form as you like.
Another alternative is to do custom ModelForm and define a field with a widget that renders two input fields, in the form's .save() method, set the widget resulting value (a tuple) to both fields.
这对我有用
this has worked for me
这很愚蠢,但是,是的,如果您要使用 tuple-within-a-tuple 方法,则必须指定应在表单上显示的所有字段。
It's stupid, but yes, if you're going to use the
fieldsets
tuple-within-a-tuple method, you have to then specify all the fields that should show on your form.同意,这很烦人,但是它是字段列表中的元组的元组。
您可以使用列表理解并将列表更改为元组。
这是一个跳过某些字段的示例,您希望在包括正常休息方式的同时给予一些特别注意。
经过一些小的调整,这应该可以工作。
Agreed, that its annoying, but its tuple of tuples from list of fields.
you can use list comprehension and change list to tuple.
Here is an example for skipping some fields, that you want to give some special attention WHILE including rest normal way.
with small tweaking this should work.
将这些字段包装在它们自己的元组上。
在上面的示例中,字段
field2
和field3
显示在一行上。Wrap those fields on their own tuple.
In the above example, fields
field2
andfield3
are shown on one line.