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

44đź‘Ť

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.

👤Sandy

2đź‘Ť

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.

👤Jj.

2đź‘Ť

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>
👤Serjik

2đź‘Ť

this has worked for me

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

1đź‘Ť

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.

👤Chris Pratt

1đź‘Ť

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.

👤Deil

Leave a comment