3

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.

bituze
  • 75
  • 6

1 Answers1

2

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)
pistolpete
  • 968
  • 10
  • 20