Setting Up the Django Admin
00:00 Setting Up the Django Admin. The Django admin provides a web-based interface for creating and managing database model objects. To see it in action, you’ll first need a Django project and some object models.
and a new Django project named
School is created, followed by an app called
core. Then you migrate the necessary tables for the Django installation and create an administrator. This is the
core app directory starts with the following files inside. You’re interested in two of these files: firstly,
models.py, which defines the database models; and secondly,
admin.py, which registers the models with a Django admin. To demonstrate the outcome when customizing the Django admin, you’ll need some models. Here, you see editing of
03:09 Here’s a model diagram showing the relationships between the objects. the underlying table names in the database are slightly different from this, but they’re related to the models shown above. Each model that you want Django to represent in the admin interface needs to be registered.
This is done in the
admin.py file. Models from
core/models.py are registered in the corresponding
admin.py file. As you can see, this code registers the three models we’ve created—
03:53 And with that, you’re almost ready to go. Next up, back on the command line, we’re making and applying the migrations for those models. Once those have been applied, you can run the Django development server and see the results.
Once the development server is running, visit the address which was seen onscreen to see the admin interface. You’ll be prompted to log in, and you need to use the credentials you created with the
createsuperuser management command. As you can see, the admin home screen lists all of the registered database models: Courses, Grades, and People.
04:36 You can now use the interface to create objects in the database. Clicking a model name will show you a screen listing all of the objects in the database for that model. Here People was clicked, leading to the list of people.
04:49 As you can see, the list starts out empty, like the database. Clicking ADD PERSON allows you to create a person in the database. And once you save, you’ll be returned to the list of Person objects.
The good news is you’ve got an object. The bad news is
Person object (1) tells you the ID of the object and nothing else. By default, the Django admin displays each object by calling
str() on it.
Become a Member to join the conversation.