简体   繁体   English

Django admin 在同一行显示多个字段

[英]Django admin display multiple fields on the same line

I have created a model, it will automatically display all the fields from the model and display it on the admin page.我创建了一个 model,它会自动显示 model 中的所有字段并显示在管理页面上。

Now, I have a problem, I would like to have two fields on the same line, to do this I have to specify the fieldsets at ModelAdmin:现在,我有一个问题,我想在同一行上有两个字段,为此我必须在 ModelAdmin 中指定字段集:

fieldsets = (
        (None, {
            'fields': (('firstname', 'lastname'),)
        }),
       )

Do I have to specify all the fields?我必须指定所有字段吗? Because there are many fields in the database I need to specify.因为数据库中有很多字段我需要指定。

Wrap those fields on their own tuple.将这些字段包装在它们自己的元组上。

class TestAdmin(admin.ModelAdmin):
    fields = (
        'field1',
        ('field2', 'field3'),
        'field4'
    )

In the above example, fields field2 and field3 are shown on one line.在上面的示例中,字段field2field3显示在一行上。

There is an article may be useful有一篇文章可能有用

http://amk1.wordpress.com/2010/09/23/a-2-column-django-admin-form/ http://amk1.wordpress.com/2010/09/23/a-2-column-django-admin-form/

Article is quote below:文章引用如下:


Django is great. Django 很棒。 The bundled admin interface makes it better.捆绑的管理界面使它变得更好。 But as the number of items on the form gets bigger, the amount of wasted space increases because the layout is single column.但是随着表单上的项目数量越来越多,浪费的空间量也会增加,因为布局是单列的。 Coupled with left alignment on wide-screen monitors, my users usually end their day with a condition we call “eyeballs misalignment”.再加上宽屏显示器上的左 alignment,我的用户通常会以我们称之为“眼球错位”的情况结束他们的一天。

So I improvised and changed the form (and StackedInline) to a 2-up layout.所以我即兴创作并将表单(和 StackedInline)更改为 2-up 布局。 No more “eyeballs misalignment”.不再有“眼球错位”。

The corresponding template for Django 1.2.1 (/contrib/admin/templates/admin/includes/fieldset.html) looks like this, modified lines highlighted: Django 1.2.1 (/contrib/admin/templates/admin/includes/fieldset.html) 的相应模板如下所示,修改后的行突出显示:

 <fieldset class="module aligned {{ fieldset.classes }}"> {% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %} {% if fieldset.description %} <div class="description">{{ fieldset.description|safe }}</div> {% endif %} <table border=0 width=100%> {% for line in fieldset %} {% cycle '<tr>' '' %} <td width=50%> <div style="border-bottom:0" class="form-row{% if line.errors %} errors{% endif %}{% for field in line %} {{ field.field.name }}{% endfor %}"> {{ line.errors }} {% for field in line %} <div{% if not line.fields|length_is:"1" %} class="field-box"{% endif %}> {% if field.is_checkbox %} {{ field.field }}{{ field.label_tag }} {% else %} {{ field.label_tag }} {% if field.is_readonly %} <p>{{ field.contents }}</p> {% else %} {{ field.field }} {% endif %} {% endif %} {% if field.field.field.help_text %} <p class="help">{{ field.field.field.help_text|safe }}</p> {% endif %} </div> {% endfor %} </div> </td> {% cycle '' '</tr>' %} {% endfor %} </table> </fieldset>

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.一种选择是覆盖该 ModelAdmin 的 change_form.html 模板,并根据需要设置表单样式。

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.另一种选择是自定义 ModelForm 并使用呈现两个输入字段的小部件定义一个字段,在表单的 .save() 方法中,将小部件结果值(一个元组)设置为两个字段。

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.这是跳过某些字段的示例,您需要特别注意其中包括 rest 正常方式。

skipped=[]
alist = [field.name for field in <model_name>._meta.fields if field.name not in skipped]
fieldsets = tuple(alist)
*** play with skipped ***

with small tweaking this should work.稍作调整,这应该可以工作。

this has worked for me这对我有用

fieldsets=(        
       ("My Group",{"fields": (tuple(['field1','field1']),),}), 
    )

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.这很愚蠢,但是是的,如果您要使用fieldsets tuple-within-a-tuple 方法,那么您必须指定应该在表单上显示的所有字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM