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

发布于 2024-11-04 15:04:55 字数 285 浏览 6 评论 0 原文

我创建了一个模型,它将自动显示模型中的所有字段并将其显示在管理页面上。

现在,我有一个问题,我想在同一行上有两个字段,为此我必须在 ModelAdmin 中指定字段集:

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

我必须指定所有字段吗? 因为有我需要指定数据库中的许多字段。

I have created a model, it will automatically display all the fields from the model and display it on the admin page.

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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

秋凉 2024-11-11 15:04:56

有一篇文章可能有用

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

文章引用如下:


姜戈很棒。捆绑的管理界面使其变得更好。但随着表单上的项目数量越来越多,浪费的空间量也会增加,因为布局是单列的。再加上宽屏显示器上的左对齐,我的用户通常会以我们称之为“眼球错位”的情况结束他们的一天。

所以我即兴创作并将表单(和 StackedInline)更改为 2-up 布局。不再有“眼球错位”。

Django 1.2.1 的相应模板 (/contrib/admin/templates/admin/includes/fieldset.html) 如下所示,修改后的行突出显示:

{% if fieldset.name %}

{{ fieldset.name }}

{% endif %} {% if fieldset.description %}
{{ fieldset.description|safe }}
; {% 结束 %} <表格边框=0宽度=100%> {% for fieldset % 中的行} {% 循环 '' ''%}
{{ 行. 错误 }} {% 表示第 % 行中的字段} {% if field.is_checkbox %} {{ 字段.field }}{{ 字段.label_tag }} {% 别的 %} {{ 字段.label_tag }} {% if field.is_readonly %}

{{ field.contents }}

{% 别的 %} {{ 字段.字段 }} {% 结束 %} {% 结束 %} {% if field.field.field.help_text %}

{{ field.field.field.help_text|safe }}

{% 结束 %}
{% 结束 %} {% 循环 '' '' %} {% 结束 %}

There is an article may be useful

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

Article is quote below:


Django is great. 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”.

So I improvised and changed the form (and StackedInline) to a 2-up layout. 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:

<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>
夏有森光若流苏 2024-11-11 15:04:56

恐怕没有一个简单的方法可以做到这一点。

一种选择是覆盖该 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.

挖个坑埋了你 2024-11-11 15:04:56

这对我有用

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

this has worked for me

fieldsets=(        
       ("My Group",{"fields": (tuple(['field1','field1']),),}), 
    )
孤君无依 2024-11-11 15:04:56

这很愚蠢,但是,是的,如果您要使用 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.

°如果伤别离去 2024-11-11 15:04:56

同意,这很烦人,但是它是字段列表中的元组的元组。
您可以使用列表理解并将列表更改为元组。
这是一个跳过某些字段的示例,您希望在包括正常休息方式的同时给予一些特别注意。

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

经过一些小的调整,这应该可以工作。

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.

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.

海未深 2024-11-11 15:04:55

将这些字段包装在它们自己的元组上。

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

在上面的示例中,字段 field2field3 显示在一行上。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文