Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

594
Vistas
Django dynamic nested model forms

Suppose I have the following models, where there is a 1:many relationship from Teacher to Course, and from Course to Student:

class Teacher(Model):
    name = CharField(max_length=64)
    degree = CharField(max_length=64)

class Course(Model):
    title = CharField(max_length=64)
    level = CharField(max_length=64)
    teacher = ForeignKey(Teacher, on_delete=CASCADE)

class Student(Model):
    name = CharField(max_length=64)
    year = CharField(max_length=64)
    course = ForeignKey(Course, on_delete=CASCADE)

If I want to have a "register teacher" page, where a user can input the details of a Teacher, and also be able to add any number of Courses with their corresponding details, and to each Course add any number of Students, how could this be done? I am aware of ModelForms, which would make constructing a basic form for a single instance of a model rather trivial, but how can I create a form or forms that essentially nests the models within each other, and allows for a dynamic number of Course and Student?

For example, within the Teacher form, there is an "add Course" button, that adds another Course form under that Teacher, such that any details entered into it populate a Course that belongs to that Teacher, and then for each Course form, there is an "add Student" button that adds a Student form for a Student that belongs to that Course. How can I implement this dynamic and nested behavior?

Then if it is possible to make such a form, back on the Python side after the form is submitted, how will the data be organized? Or in other words, what will indicate to which Course each Student belongs?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You should look at Django ModelFormsets, it is used in those cases

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda