How to Register a Database Table with the Admin Interface in Django


In this article, we show how to register a database table with the admin interface in Django.

What is meant is, say that we've created a database table in Django through the file and we've made the migrations so that the database table is synched with all code in the project directory.

We can control and interact with this database table in the admin interface, which is found at ipaddress/admin or

Through the admin interface, we can do things such as delete records from a table, add new records to a table, update values in a table, and so on and so forth. And the admin interface is extremely easy to use and work with in Django, so this is a great feature.

However, to be able to interact with a database table that you've created, you need to register the database table, so that is shows up in the admin interface.

So, how can this be done?

So you've created a database table in a certain app. For example, you may have created this database table in the Articles app.

So you go to this app and open the file.

It is in this file that you're going to register your database table, so that it can appear in the admin interface and you can do things such as add, delete, and update records.

So, say you've created a table named Students.

The following code, shown below, is how you would register the table Students in the file.

So it's very simple code.

So, first you must import admin from django.contrib. This normally in the file by default.

Then, you must import the table you want to set up an admin interface for.

Since database tables are created in the file, we import the table from models.

So now that the table is imported, we can now register it.

We do this through the line,

Now, the table, Students, is registered. So, if you open up the admin page of your website, you should see the Students table on the interface. You can now do things such as add records to the table, update records, and delete records. It's very simple, very easy.

Registering Multiple Database Tables with Admin

Of course, you don't have to register a single table. You can register all of your database tables at once, at least all the database tables are present within the app that you're in.

Say, you created 3 database tables, Students, Books, TableContents.

The code to register all of these 3 tables, so that they appear and be edited with the admin interface is shown below.

So the code is the same, except we now import 3 tables. And we register 3 database tables.

Now, if you go to the admin page of your website, you should see something such as that shown below.

Django admin site- database tables

You see that Django adds an extra 's' to the table names by default. This is something you don't have to worry about. None of the functionality of the database tables has changed. It's simply a convention that Django does.

If you click on any of the table names, you'll see that you can add records, update records, delete records, and so on.

So registering database tables to the admin interface is a powerful tool in Django that lets the admin of the site easily control data of the website, which is the whole key.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...