Eyal
Eyal

Reputation: 1709

How to change the layout and fields disposition of Django's Admin forms?

I have a simple model of a Student which is registered to the admin through a admin.ModelAdmin.

The admin change form of this model looks like this: Current look

I'd like to change the layout of this form a little so that a few fields could be in the same line, like so (edited in Paint): enter image description here

Is there something I could do without overriding the default templates? And if there isn't, what's the best way?

Thanks in advance.

Upvotes: 4

Views: 2432

Answers (2)

GwynBleidD
GwynBleidD

Reputation: 20569

You can use fields attribute in ModelAdmin like this:

@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):

    fields = (
        ('no_student', 'last_name', 'first_name),
        'course',
        'sex',
        'id'
    )

Fields grouped in one sub-tuple or sub-list will be shown in one line. You can create more than one groups like this.

Upvotes: 8

Sagar Ramachandrappa
Sagar Ramachandrappa

Reputation: 1461

You can do this by using fieldsets.

class StudentAdmin(admin.ModelAdmin):
    fieldsets = (
        (None, {
            'fields': (('no_student', 'last_name', 'first_name), 'course', 'sex', 'ID')
        }),
        ...
)

Please note i have wrapped the fields which should be shown in single line in same tuple.

Reference

Upvotes: 2

Related Questions