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.
00:15 First, you’ll see creation of a fresh virtual environment and installation of Django. The first command creates a new virtual environment and the second one switches to it.
00:28 Now that the command to activate the virtual environment works on macOS and Linux, but you’ll need a different one for Windows, as you’re seeing onscreen now.
00:39 Next, Django is installed,
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
Access to the Django admin screens is restricted to users with
superuser flags, so you use the
createsuperuser management command to create a
Next, you’ll also need to modify
School/settings.py to include the new app named
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
First, you’ll see creation of a
Person, which has a first and last name and can take zero or more courses.
Next, the courses. Each
Course has a
name and a
year in which it was offered.
Grade contains a percentage score that a
Person achieved on a course.
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.
You can make this screen a little more helpful by adding a
.__str__() method to the
Person class in
In this case, the
.__str__() method added to
Person changes the display to include the first and last name of the
Person in the interface.
Refreshing the screen once this code has been saved will show you the change. That’s a little better. Now you can see some information about the
It’s a good idea to add similar methods to both the
Course and the
06:15 You’ll want to have some data in your database to see the full effect of your customizations. You can have some fun and create your own data now, or you can skip the work and use a fixture.
Django lets you load data to and from the database in files called fixtures. Enter the following into a file named
Once you’ve created the file or used the one from the provided resources, you can use the Django management command
loaddata to load it into your database.
Your database now has some sample
Grade objects. Now that you have some data to work with, you’re ready to start customizing Django’s admin interface.
Become a Member to join the conversation.