Previous Page Next Page

Viewing Multiple Models Inline

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 Interface

In 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:

1.
Stop the development server.

2.
Use the following command from the root of the iFriends project to create a Poll application:

python manage.py startapp Poll

3.
Create and open the iFriends/Poll/models.py file in an editor.

4.
Add the following import statement, shown in Listing 17.4, to import the Person model:

from iFriends.People.models import Person

5.
Add the following lines of code to define the UserPoll model, shown in Listing 17.4, with question, date, and person fields:

class UserPoll(models.Model):
    question = models.CharField('Question', max_length=100)
    date = models.DateTimeField('Creation Date')
    person = models.ForeignKey(Person)

    def __str__(self):
        return '%s' % (self.question)
    class Admin:
        pass

6.
Add the following lines of code to define the Opinion model, shown in Listing 17.4:

class Opinion(models.Model):
. . .
    def __str__(self):
        return '%s' % (self.opinion)

By the Way

We do not include the Admin class in the Opinion model because it will be displayed in the Admin class of the UserPoll object. We do not want it to be displayed on its own in the admin interface, because Opinion objects are always added with the UserPoll objects.

7.
Add the following lines of code that add the poll field to the Opinion model, as shown in Listing 17.4. The poll field links the Opinion model to the UserPoll model as a foreign key. The edit_inline option is set to TABULAR so that this model is displayed as a table in UserPoll model forms. The num_in_admin option is set to 4, so four instances are automatically added to the UserPoll forms:

poll = models.ForeignKey(UserPoll, edit_inline=models.TABULAR,
  num_in_admin=4)

8.
Add the following fields to the Opinion model to define the opinion and vote count:

opinion = models.CharField('Opinion', max_length=50, core=True)
votes = models.IntegerField('Vote Count', core=True)

9.
Save the iFriends/Poll/models.py file.

10.
Open the iFriends/settings.py file in an editor.

11.
Add the following line to the INSTALLED_APPS setting to enable the Poll application:

'iFriends.Poll',

12.
Save the iFriends/settings.py file.

13.
Use the following command at the root of the iFriends project to synchronize the database:

python manage.py syncdb

14.
Start the development server.

15.
Access the add form for a UserPoll object in the admin interface, as shown in Figure 17.8, to see the inline Opinion model as well.

Figure 17.8. The UserPoll add view in the admin interface with four instances of the Opinion model displayed inline.


Listing 17.4. Full Contents of the iFriends/Poll/models.py File

from django.db import models
from iFriends.People.models import Person

class UserPoll(models.Model):
    question = models.CharField('Question', max_length=100)
    date = models.DateTimeField('Creation Date')
    person = models.ForeignKey(Person)
    def __str__(self):
        return '%s' % (self.question)

    class Admin:
        pass

class Opinion(models.Model):
    poll = models.ForeignKey(UserPoll, edit_inline=models.TABULAR,
           num_in_admin=4)
    opinion = models.CharField('Opinion', max_length=50, core=True)
    votes = models.IntegerField('Vote Count', core=True)

    def __str__(self):
        return '%s' % (self.opinion)


Previous Page Next Page