Another useful model customization of the admin interface is to display in the same form multiple models that link to each other. In the add and update forms in the admin interface, you can configure models with a ForeignKey field link to another model to be displayed with that model.
To view multiple models inline in the admin interface, add the edit_inline argument to the ForeignKey definition. The edit_inline argument can be set to models.TABULAR or models.STACKED.
The models.STACKED option displays the fields stacked on top of each other in the form. The models.TABULAR option displays the fields next to each other in a single table row. Which option you use depends on what fields exist in the model.
You can define the number of instances of the model to be included in the form by setting the num_in_admin argument. You also need to add the core=true attribute to the other fields in the model.
The following example shows how to implement the edit_inline argument to display a model inline with another model:
class Quote(models.Model): text = models.TextField('text', max_length=200, core=True) by = models.CharField('by', max_length=50, core=True) person = models.ForeignKey(Person, edit_inline=models.TABULAR)
Try It Yourself: Configure an Inline Model in the Admin InterfaceIn this section, you will create a new Poll application that will include UserPoll and Opinion models. You will define the models so that they can be edited inline in the admin interface. Follow these steps to create the Poll application and define the models:
Listing 17.4. Full Contents of the iFriends/Poll/models.py File
|