The admin interface - 30 days of Django

The admin interface - 30 days of Django

/ #Django


Learn how to use the built in Django admin interface to add data to the database.

Django comes with a brilliant admin interface. This can be used to add, edit, delete and view all of the data connected to our project. We can also add custom features and similar.

Let's say that we wanted to add a few tasks. We can use the shell or we can do this programmatically, but that's not very convenient right now. So let's create a superuser and log in.

Creating a superuser

Django comes with an authentication system. And this system has roles and permissions out of the box. So if you create a new user, that user will not automatically have access to the admin interface. We need permissions to do this. So in our case, we're going to create something called a superuser.

Let's go to the command line, and run this command:

$ python3 manage.py createsuperuser

Just follow the wizard and select a username, password and email. When that's done, we can run the webserver again.

$ python3 manage.py runserver

Btw, the admin interface is actually one of the other predefined INSTALLED_APPS inside the settings.py file. You can also see in the urls.py file that the admin interface has been added there.

Once you're logged in, you will see that we can see users and groups. If you click the users link, you will see the superuser you just created. If you want to do some changes to it, just give it a try :-)

Registering the task model

So where is the "task" model? Eventhough we registered our app in INSTALLED_APPS and similar, it doesn't automatically appear here in the admin interface. But luckily for us, it's very easy to do so.

Open up the file "task/admin.py" and add the following content to it:

from .models import Task

admin.site.register(Task)

First, we import the database model. We can use ".models" to refer to the models.py file since we're in the same folder. Next, we use a function from django to register the model.

If you go back to the admin interface and refresh now, you will see that the task model is there. Go ahead and add a few test tasks.

We will come back to the admin interface later and do some customizations here. We are going to add search, filters and some other cool things.

Table of contents

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.