So you’ve made a new Model in Django and you’re eager to see it in action. You’ve done the usual steps: make migrations, migrate the database, and create a superuser. You run the server and go to http://127.0.0.1:8000/admin/
to log in and start playing with your model data. But wait, where is it? Why can’t you see it in the admin interface?
Don’t panic, you’re not alone. This is a common mistake that many new Django users make. You see, Django doesn’t automatically register your models to the admin interface. You have to do it manually, but it’s not hard at all. In fact, it’s very simple and straightforward. And this article will show you how to do it in no time.
By the end of this tutorial, you’ll be able to register any model to Django’s admin interface and customize it as you wish.
Ready? Let’s go.
Table of Contents
- How To Register A Model In Django Admin
- How To Register All Models In Django Admin(Same App).
- How To Register All Models In Django Admin (All Apps).
- Conclusion
How To Register A Model In Django Admin
To register a model to Django’s admin, first import the model into the admin.py
file of the same Django app as the models.py
file. Then use admin.site.register(ModelName)
to register that model into Django’s admin and you’ll be done.
Here is an example.
Let’s assume you’re working on a Django blog application and you’ve got the following models in your models.py
file.
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
class Author(models.Model):
User = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField()
def __str__(self):
return self.user.username
class Category(models.Model):
title = models.CharField(max_length=20)
def __str__(self):
return self.title
class Post(models.Model):
title = models.CharField(max_length=50)
overview = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)
comment_count = models.IntegerField(default=0)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
thumbnail = models.ImageField()
categories = models.ManyToManyField(Category)
def __str__(self):
return self.title
Don’t get messed up with all this code.
I’ll explain briefly what it is doing and then get into how to register the models to Django’s admin panel. The reason I used such a seemingly complex example is that things are not always as simple as one would think. So this example is a projection of what you may face in your future Django projects. Nevertheless, it is not as complicated as it looks. It’ll all make sense after I explain.
If you are at this point of wanting to know how to register your Django models to the admin, the models above should already be making sense to you.
In line ➊ we import models, from which our models will inherit.
Since we are going to use Django’s built-in User class, we import it in line ➋ and we create an instance of the class in line ➍.
As you can see, we have created 3 models, namely Post, Author, and Category. Let’s now register these models in the admin.py file.
Let’s register the Post model first since it is the most important on this list.
from django.contrib import admin
from .models import Post
admin.site.register(Post)
If you run the server and go to http://127.0.0.1:8000/admin/ and log in, you should be able to see the Post model in the admin panel.
But the above example only dealt with one model, you may be asking yourself how multiple models can be registered all at once. Well, it’s as easy as we just did. You might have already guessed it, that is to import the model and add another line registering it using admin.site.register(ModelName)
.
from django.contrib import admin
from .models import Post, Category, Author #new
admin.site.register(Post)
admin.site.register(Category) #new
admin.site.register(Author) #new
The last two lines are registering the other two models.
This is how your admin will end up looking.
You’ll see that all of these models are displayed under the POST title that’s because it is the name of the app from which the models belong. If you’d have registered models of a different app in their admin.py
file, they’d have been displayed under their corresponding app. Note that the models are, by default, displayed in the order in which they are written in models.py
.
How To Register All Models In Django Admin(Same App).
What if you want to automate this whole process of registering models to admin in Django, that is, to be able to register all models in an app without having to write a registration line of code for each one of them.
Let’s see how to do it. Put the following code in the admin.py
file of the app concerned.
from django.contrib import admin
from django.apps import apps
post_models = apps.get_app_config('post').get_models()
for model in post_models:
try:
admin.site.register(model)
except admin.sites.AlreadyRegistered:
pass
Let me explain what this code is doing.
In line ➋ we import the apps which among other things contain a list of all models available in every app.
In line ➍, we create a variable called post_models
which stores a list of all the models available in the app. In this case, it is the ‘post,’ app which is passed in the get_app_config()
method.
Line 5 starts a for loop that registers a model to Django if the model has not been registered. This is done in case you plan to register other models manually above the for loop.
But if you do not, you can simply use the following code:
from django.contrib import admin
from django.apps import apps
post_models = apps.get_app_config('post').get_models()
for model in post_models:
admin.site.register(model)
What we just did was remove the try-except block that checks if a model is already registered.
This means that when you create a new Django model in that app, you no longer have to register it manually. The code above will do the same thing for you.
However, all the above code works for one app, what if you want to do it for all apps. The next section will show you how to do that.
How To Register All Models In Django Admin (All Apps).
If you want to register all models in Django admin all at once you’ll never need to register another model ever again in whatever Django app in your project. Do the following.
First, create an admin.py
file in your project folder so that it will look like something below.
After this, go into the settings.py
of the same folder and include the folder in the list of installed apps.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'post',
'blog' # new (project folder name)
]
Now in the admin.py file, put the following code.
from django.contrib import admin
from django.apps import apps
all_models = apps.get_models()
for model in all_models:
try:
admin.site.register(model)
except admin.sites.AlreadyRegistered:
pass
Now run the server, and check your admin panel.
Conclusion
That was it fellow Django developer. You’ve learned how to register models in Django admin and then how to automatically register all models as well. I hope you were able to follow along with this guide to the point of doing it all alone.
If you were not, or if you identified some errors, please let me know in the comments section below. Otherwise, share this post with fellow Django newbies whom you think will need it.
I’ll see you in other Coding Gear Guides. Peace!
In fact this site is really a good place to learn Django. Because you guys explain each code better which help us to understand what is going on in the code.
Thanks Vasee. I’m glad you liked this content. we’re always publishing new and helpful content here. You can also come back for more