Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin Fieldsets

Trying to understand Django Admin a bit better, but I find the Django documentation a bit lacking sometimes (or perhaps my capacity to understand).

I know you can use fieldsets to control the layout of certain admin pages. What I can't seem to grasp is what the fieldset names are.

If I have the following class

class Demo(model.Model):
    name = models.CharField(max_length=150)
    address = models.CharField(max_length=150)
    city = models.CharField(max_length=50)
    zip = models.CharField(max_length=15)

and Admin class as the following

class DemoAdmin(admin.ModelAdmin):
    list_display = ('name', 'City')

In this, albeit contrived example, what possible fieldsets could I use?

like image 452
Consiglieri Avatar asked Sep 17 '09 10:09

Consiglieri


1 Answers

Try this, and you'll soon see how it looks/works.

class DemoAdmin(admin.ModelAdmin):
  list_display = ('name', 'city')
  fieldsets = (
      ('Standard info', {
          'fields': ('name')
      }),
      ('Address info', {
          'fields': ('address', ('city', 'zip'))
      }),
   )

When you go to the change-page to edit, you'll get one box "standard info" with the name-box. And you'll get another box that says "address info" with the adress field first, and then the city and zip-fields on the same line after.

like image 97
odinho - Velmont Avatar answered Oct 06 '22 00:10

odinho - Velmont