[Answered ]-Django Dynamic Nested Formsets

1👍

This is a simple example of what you could do. It is not exactly nested. But it will provide most of the functionality you are looking for.

# admin.py
from django.contrib import admin
from .models import Clinic, Doctor, DoctorHours


class DoctorHoursInline(admin.TabularInline):
    model = DoctorHours


@admin.register(Doctor)
class DoctorAdmin(admin.ModelAdmin):
    # stuff
    inlines = [DoctorHoursInline,]
    # more stuff


class DoctorInline(admin.TabularInline):
    model = Doctor

    # This will show a link to edit the Doctor model
    # in the Doctor table of the Clinic change form.
    show_change_link = True 


@admin.register(Clinic)
class ClinicAdmin(admin.ModelAdmin):
    # stuff
    inlines = [DoctorInline,]
    # more stuff

You may of course want to swap out the forms on each of these ModelAdmin and TabularInline classes.

Docs: https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#inlinemodeladmin-objects

Leave a comment