Reorganizing the Form Fields

If you tried adding the Virtual Host instances using the current administration interface, you probably noticed how unfriendly and confusing the process is. First you have to create a new VirtualHost object; then you have to navigate away from it and create one or more VHostDirective objects by picking the newly created Virtual Host object. Wouldn't it be nicer if you could create all that from one form? Luckily this is very easy thing to do. In Django terms this is called an inline formsetand allows you to edit models on the same page as the parent model.

In our case, the parent model is the VirtualHost and we want to edit the instances of the VHostDirective inline. This can be accomplished in only two steps. First you create a new administration class that inherits from the admin. Tabularlnline class. Add the following code to the admin. py file. The properties of this class indicate which child model we want to include and how many extra empty lines we want to have in the formset:

class VHostDirectiveInLine(admin.TabularInline): model = VHostDirective extra = 1

The second step is to instruct the administration class that you want to have this inline form included in the main model edit form:

class VirtualHostAdmin(admin.ModelAdmin): inlines = (VHostDirectiveInLine,) [... ]

This simple manipulation results in a rather nice looking formset that includes the entry fields for both the parent and the child models, as shown in Figure 5-3.

^ ^ ^ Change Virtual Host I Django site admin

+ i^http//

Chang« Virtual Host I Django site

Django administration

Home Httpconfig Virtual Hosts Another f

Change Virtual Host

Q Is default 0 Is template

Description: Another test server Bind address: • 8080

Welcome, rytls. Change password / Log out


ServerNeme Mrww.9009le.c0m

Server Al iat

-- î ♦

II Delete

Save and add another Save and continue editing £

Figure 5-3. Including the child model editing form

If you don't like the way the fields are organized in the form, you can change their order and also group them into logical groups. You group the fields by defining the fieldsets. Each fieldset is a tuple of two elements: a fieldset name and a dictionary of fieldset properties. One dictionary key is required, the list of fields. The other two keys, classes and description, are optional. Following is an example of the ConfigDirective model administration form, which has two fieldset groups defined:

class ConfigDirectiveAdmin(admin.ModelAdmin): fieldsets = [

('Details', {'fields': ['is_container', 'documentation'], 'classes': ['collapse'],

'description': 'Specify the config directive details'})

The first group contains only one field and has no name. The second group is labeled 'Details'. It has a short description below the label, contains two fields, and has a show/hide capability.

The ' classes' property defines the CSS class name and depends on the class definitions. The standard Django administration CSS defines two useful classes: the ' collapse' class allows you to show/hide the whole group and, the 'wide' class adds some extra space for the form fields.

Was this article helpful?

0 0

Post a comment