alt-text

Adding new Models to the Admin site in Django

So you have just created a custom model for your Django application and executed the database migrations. You spin up your local server and navigate to the admin site but your model isn't there? Why not?

There is actually one more required step. We need to register our model with our admin site. Let's go through a quick example.

Let's create a posts app inside our Django project.

python manage.py startapp posts

Once we execute our startapp command, a new posts directory will be created and will contain models.py. This is where we define our Post model.

# models.py
class Post(models.Model):
    title = models.CharField(max_length=255)
    # additional fields if needed

After running our database migrations, our Post model is ready to use.

python manage.py makemigrations
python manage.py migrate

However, if we navigate to our admin site (usually at http://localhost/admin) we can see that our model is not there (yet). To fix this we need to update the admin.py file inside our posts directory. This is where we will register our new Post model.

# admin.py
from django.contrib import admin
from .models import Post


admin.site.register(Post)

That's it! Now when we navigate to the admin site we should see our Post model and be able to manage any new or existing post objects.

custom model on django admin site