Django admin multiple querysets on one page


March 2019


19 time


How to have multiple tables(queryset) on one page in django admin.

For example:

When i go to the company page, i can see the list of departments in the company, i can also see the list of employees in the company.

1 answers


You can use InlineModelAdmin objects to implement this, although if you want to do nested inlines you should check out this post. Although as that post says: would be a kind of convoluted design to implement.

You didn't provide any code here so the best I can do is guess your model relationships.

from django.db import models

class Department(models.Model):
    name = models.CharField(max_length=250)

class Employee(models.Model):
    name = models.CharField(max_length=250)

class Company(models.Model):
    name = models.CharField(max_length=250)
    departments = models.ForeignKey(Department)
    employees = models.ForeignKey(Employee)

from django.contrib import admin

class EmployeeInline(admin.StackedInline):
    model = Employee

class DepartmentInline(admin.StackedInline):
    model = Department

class CompanyAdmin(admin.ModelAdmin):
    list_display = ('name')
    inlines = [DepartmentInline, EmployeeInline]