Previous Page Next Page

Activating the Model in the Admin Interface

Models are not visible in Django's admin interface automatically. You must activate a model for it to be accessible from the admin interface. The admin interface provides an easy way to add and edit objects in the model. However, it is not necessary to activate all or any models for Django to function.

The Admin class must be added to the model to activate the model in the admin interface. Only a simple Admin class is required:

class Admin:
     pass

Specific items that can be defined in the Admin class will be discussed in Hour 17, "Customizing Models in the Admin Interface."

Try It Yourself: Activate the People Model in the Admin Interface

In this section, you activate the People application you created in Hour 2 to make it accessible in the admin interface.

1.
Open the iFriends\People\models.py file in an editor.

2.
Add the Admin class to the Person class, as shown in Listing 3.1, to activate the Person model in the admin interface.

Listing 3.1. Full Contents of the iFriends\People\models.py File with the Admin Interface Activated

from django.db import models

class Person(models.Model):
    name = models.CharField('name', max_length=200)
    desc = models.TextField('Desc', max_length=500)

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

    class Admin:
        pass

3.
Save the file.

4.
Refresh your web browser. The People application should be available, as shown in Figure 3.3



Figure 3.3. Main page for Django's admin interface, showing the People application.


Did you Know?

You don't have to stop the Django server and log in again to the admin interface to allow the Django admin to access a model that has been activated. All you need to do is refresh the web browser within the admin interface.



Previous Page Next Page