Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 367
kelvinfix Avatar asked May 02 '11 00:05

kelvinfix


People also ask

What is Inlines in Django?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.

What is Fieldsets in Django admin?

fieldsets is a list of two-tuples, in which each two-tuple represents a <fieldset> on the admin form page. (A <fieldset> is a “section” of the form.)


2 Answers

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.

like image 58
Sandy Avatar answered Sep 22 '22 01:09

Sandy


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.

like image 26
Jj. Avatar answered Sep 21 '22 01:09

Jj.