👨‍💻 Looking for a developer? I'm available for work. Contact me at contact@ademhodzic.com
Get Django Saas Blog

How to Display Many Django Models in One Admin Page (With Examples)

Published on: July 27, 2022

It's common to have a relationship between models but it doesn't make sense to have seperate pages on Django admin page for them.

Good example you have a parent model that has a lot of children and you want to manage these records via one page in Django admin.

A quiz site is a good example. Let's say you have two models Question and Answer. One Question can have multiple Answers.

# questions/models.py from django.db import models class Question(models.Model): text = models.TextField( null=False, blank=False, ) class Answer(models.Model): question = models.ForeignKey( Question, related_name="answers", on_delete=models.CASCADE, ) text = models.CharField( verbose_name=_("answer"), max_length=150, ) correct = models.BooleanField(default=False)

And you want to be able to update all Answers for a Question in one place. This is a pretty common situation so Django has it solved out of the box.

Django Admin Inlines to the Rescue

The admin interface has the ability to edit models on the same page as a parent model.

Let's create an example admin.py to demonstrate Django inlines.

# questions/admin.py from django.contrib import admin from questions.models import Question, Answer class AnswerInline(admin.TabularInline): model = Answer extra = 1 class QuestionAdmin(admin.ModelAdmin): inlines = [AnswerInline] admin.site.register(Question, QuestionAdmin)

If you've followed these steps correctly, you should see the following page when you want to create a new Question record:

Django Inline Example

Resources