Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

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,

00:48 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 superuser.

01:13 Access to the Django admin screens is restricted to users with staff or superuser flags, so you use the createsuperuser management command to create a superuser.

01:25 Next, you’ll also need to modify School/settings.py to include the new app named core.

01:41 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 core/models.py.

02:19 First, you’ll see creation of a Person, which has a first and last name and can take zero or more courses.

02:36 Next, the courses. Each Course has a name and a year in which it was offered.

02:50 The 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.

03:24 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—Person, Course, and Grade.

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.

04:15 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.

05:04 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.

05:18 You can make this screen a little more helpful by adding a .__str__() method to the Person class in models.py.

05:37 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.

05:46 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 Person object.

05:56 It’s a good idea to add similar methods to both the Course and the Grade objects.

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.

06:26 Django lets you load data to and from the database in files called fixtures. Enter the following into a file named core/fixtures/school.json.

07:02 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.

07:17 Your database now has some sample Person, Course, and Grade objects. Now that you have some data to work with, you’re ready to start customizing Django’s admin interface.

07:31 And that’s exactly where we’re headed next.

Become a Member to join the conversation.