How To Register Model In Django Admin (3 Methods)

After creating a new Model, a new Django user would normally make migrations, then migrate the database, and then create a superuser so that when they run the server, they would simply go to http://127.0.0.1:8000/admin/ and login in with the hope of starting to edit the model data right away. But then they don’t find them.

Well, it turns out you’ve just forgotten to do something straightforward, yet also very forgettable. That is to register a model in Django admin and that, only that, will make your model appear in Django’s admin interface. But, worry not, for you’ve found the best article on the internet that covers how to register a model in Django admin.

Hopefully, by the end of this guide, you’d have gained enough knowledge to register your own models to Django’s admin with ease.

Let’s get started.

Table of Contents

How To Register Model In Django Admin

If you want to register a model to Django’s admin, first you have to 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

Now, don’t get messed up with all this code. Let me explain briefly what it is doing 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 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’ll 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 so that 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.

This is what your admin panel will end up looking like.

Conclusion: How To Register Model In Django Admin

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!

How To Register Model In Django Admin (3 Methods)

2 thoughts on “How To Register Model In Django Admin (3 Methods)

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top