How to display add model in tabular format in Django admin?

I’m just starting with the fact that Django is writing my first application - a char chart manager for my family. The tutorial shows how to add related objects in a table. I don't need related objects, I just want to add a regular object to 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", instead of seeing:

 Description: _____ Frequency: ______ Person: _____ 

I want him to show:

 Description: __________ | Frequency: _______ | Person: _____ 

Is it trivial, or will it take a lot of effort? And if it's easy, how can I do it?

Thanks!

+6
python django
source share
3 answers

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

Basically the following part in the link above:


The field_options dictionary can have the following keys:

fields : a tuple of field names to display in this set of fields. This key is required.

Example:

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

As with the fields parameter, to display multiple fields on the same line, wrap these fields in their own tuple. In this example, the first_name and last_name fields will appear on the same line:

 { 'fields': (('first_name', 'last_name'), 'address', 'city', 'state'), } 
+6
source share

Try something

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

Which should allow you to edit all your entries in a tabular form (if I read the documents correctly) ...

+2
source share

As far as I know, there is no default ModelAdmin parameter for this, but you can change the admin site CSS or change the change_form.html layout for each model.

You can change the admin site to use its own CSS (for each model by subclassing ModelAdmin with the Media class) and change it (possibly using the CSS body.change-form class) and make sure the fields are next to each other.

You can also create a template inside your template directory with the name /admin/chores/chore/change_form.html . Unfortunately, the part that creates the actual form elements is not in a separate block, so you must redefine the " content " block with your custom content by copying a lot of django/contrib/admin/templates/admin/change_form.html .

Further information on this can be found in the relevant documentation .

0
source share

All Articles