- Date
The Django administration site: One of the reasons why I love Django
The Django administration site is a powerful tool that let you manage the data behind your Django project. It's one of the reasons why I loved Django from the moment I tried it.
My developer friends call me “Django boy”. This is because, whenever we have to start working on a new project, I consider Django first. And even if we don’t end up using Django for the project, I will explain to them how some features could have been implemented using Django. The thing is, I fell in love with Django from the first time I tried it. There were a lot of reasons but the most important for me back then was the built in administration site.
The Django administration
Before trying Django back in 2015, I had used only PHP frameworks like Symfony, Laravel and Codeigniter. One difficulty I had with those was to populate my database so that I could test the interfaces in the early stages of the project. They didn’t provide a way to do that in a graphical user interface. So one had to insert and update data in the database using raw SQL queries or fixtures. Later on, they had some third party projects like Sonata Admin for Symfony that allowed you to add an administration site to your project. But yes, you had to install additional packages in your project and spend a lot of time configuring them.
With Django, it wasn’t the case and it is still not the case. The project that is generated by the command:
This command will create for you a project with the Django administration site already configured and ready to use. All you have to do is create your model just as usual, and then register them in the admin.
Let’s take an example. To follow along, you will need to have Python and Django installed on your computer. You also need to have some basic understanding of programming as this is not a tutorial on Django nor the Django administration site. We are going to create a project, add a model and register that model with Django admin so that we can populate the table represented by the model from the admin interface.
If you don’t have Django yet, please follow these instructions.
Creating a Django project
First make sure django is correctly installed by running the command below in your terminal.
This will show you the version of your Django installation if it was correctly installed.
To start a new Django project using your terminal, navigate to the folder where you want to create the project then run the command:
where
I won’t go in the details of each file since it is out of the scope for this article but you can learn more about them in the Django documentation.
Creating a model
A Django project is made of applications which are just ways to isolate functionalities in modules. Every model needs to be part of an application. So let's create the application that will contain our model. Change your directory to the one containing your project files (
This will create another folder called blog in your project root directory. This folder will have some files in it, including a file named
What this code does is define a new model called
Other parts of the file have been hidden for brevity.
Now, to create migrations and create tables in the database, run the two commands below:
The table where our blog entries are going to be saved is now created in the database and we can register our model to the admin site.
Registering a model to the admin site
Now let’s add our brand new model to the Django admin so that we can start creating Posts in the admin. To do that, open the
These lines tell Django that we want to be able to create Posts from the admin interface. Let’s go through each of them separately:
First, import all the Django admin classes and functions into our admin file under the namespace
Then import our Post model from the
Finally, tell Django that we want to edit Post instances in the admin and that we will configure the options available in the admin interface using the PostAdmin class. This step makes use of the register decorator from
Follow the instructions to set up a new superuser (user full access to the built in administration interface. For me it looked like this:
Now you can run the Django development server with the following command:
Check the output to know when your server is ready. It should output something like the following:
Then open the address http://localhost:8000/admin. You will see the Django administration login page:
Enter the username and the password of the user you created before then press enter. That’s it! You are in the admin interface.
You can see our blog app is registered as another app called Authentication and authorization, which is enabled by default when you create a new Django project. If you click the link Posts under our blog app, you will see a page saying select post to change. Since we haven’t added a post yet, there’s no post to change. Let’s create a new one. Click on the Add post link at the top right of the page. You should now have a form like the one below, where you can add the details of the Post you want to create:
Go ahead, fill the form and press the SAVE button at the bottom right of the page. This will create your post (if you filled the form correctly) and redirect you to the list page where you will now see your newly created post:
Django will display only one column in the list with the value Post object (id) where ID is the ID of the object in our posts table.. We can actually configure that behavior by changing specific properties in our PostAdmin class. Head to the next section to see how.
Admin options
The admin class that we decorated with the register decorator can help us customize how Django should manage our Post model in the admin. That class inherits from
List display
You can customize the fields of your model that should be displayed in the list view of the admin interface. By default, Django will use
If you did that and saved the file, go ahead and reload the posts page in your browser. You will see that Django is now displaying the title, the slug, the creation date and the is published flag for all blog posts:
List filter
Usually you will want to filter that list to display only posts that are published. With Django admin, you can configure that. Edit your admin class to add another property called
When you save your changes and refresh the list page in the browser, you will see that Django added a filter section on the right of the list of posts. There you can filter your posts by their published status or by creation date:
Search
Now, what if you had a thousands posts and you wanted to find a single one among them using its title? Django admin has you covered. Just add the
This will add a search bar to your posts lists where you can find posts that the title or the content contents what you typed in the search box:
Date hierarchy
You can also add breadcrumbs in the posts list with different dates when posts were created. For that, there is the date hierarchy property of the admin class. Edit yours to add it like here:
Your posts list should now look like this:
Conclusion
These are the 4 options of the admin interface which I always configure for my models. There are a lot more you can find in the ModelAdmin options of the Django admin site documentation. This post shouldn’t be used as a tutorial on the Django administration interface but just a highlight of its various functionalities. Thank you for reading.