Wayne Werner
Wayne Werner

Reputation: 51847

How do I display add model in tabular format in the Django admin?

I'm just starting out with Django writing my first app - a chore chart manager for my family. In the tutorial it shows you how to add related objects in a tabular form. I don't care about the related objects, I just want to add the regular object in a tabular form. This is what I have in my admin.py

from chores.models import Chore
from django.contrib import admin

class ChoreAdmin(admin.ModelAdmin):
    fieldsets = [ 
        (None,              {'fields': ['description', 'frequency', 'person']})
    ]   

admin.site.register(Chore, ChoreAdmin)

and I want when I click "add chore" that rather than seeing:

Description: _____
Frequency: ______
Person: _____

I want it to show:

Description: __________ | Frequency: _______ | Person: _____

Is this trivial to do, or would it take a lot of custom effort? And if it is easy, how do I do it?

Thanks!

Upvotes: 6

Views: 4403

Answers (3)

Raj
Raj

Reputation: 76

OP is probably all set, but for new users reading this, refer to: https://docs.djangoproject.com/en/dev/ref/contrib/admin/

Basically following part in above link:


The field_options dictionary can have the following keys:

fields: A tuple of field names to display in this fieldset. This key is required.

Example:

{
'fields': ('first_name', 'last_name', 'address', 'city', 'state'),
}

Just like with the fields option, to display multiple fields on the same line, wrap those fields in their own tuple. In this example, the first_name and last_name fields will display on the same line:

{
'fields': (('first_name', 'last_name'), 'address', 'city', 'state'),
}

Upvotes: 6

aid
aid

Reputation: 161

Something to try,

class ChoreAdmin(admin.ModelAdmin):
 list_display = ('description', 'frequency', 'person')
 list_editable = ('description', 'frequency', 'person') 

Which should enable you to edit all your entries in a tabular form (if I've read the docs correctly)...

Upvotes: 2

ralphje
ralphje

Reputation: 995

As far as I know, there's not a default ModelAdmin option for this, but you could change the CSS of the admin site or modify the layout of change_form.html on a per-model basis.

You could modify the admin site to use your custom CSS (on a per-model basis by subclassing the ModelAdmin with a Media class) and modify it (probably making use of the body.change-form CSS class) and make sure that the fieldsets are next to each other.

You could also create a template inside your templates directory with the name /admin/chores/chore/change_form.html. Unfortunately, the part that creates the actual form elements is not in a seperate block, so you should override the 'content' block with your custom contents, copying a whole lot of the django/contrib/admin/templates/admin/change_form.html file.

Please refer to relevent documentation for more information on this.

Upvotes: 0

Related Questions