kelvinfix
kelvinfix

Reputation: 3075

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.

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.

Upvotes: 24

Views: 22316

Answers (6)

Sandy
Sandy

Reputation: 883

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.

Upvotes: 46

user1640529
user1640529

Reputation: 31

this has worked for me

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

Upvotes: 2

Deil
Deil

Reputation: 106

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.

Upvotes: 1

Serjik
Serjik

Reputation: 10951

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>

Upvotes: 3

Chris Pratt
Chris Pratt

Reputation: 239380

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.

Upvotes: 1

Jj.
Jj.

Reputation: 3160

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.

Upvotes: 2

Related Questions