Django admin multiple querysets on one page

Refresh

March 2019

Views

19 time

1

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

0

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:

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

models.py

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

admin.py

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]

admin.site.register(CompanyAdmin)