Have checked the questions on this topic on SO, of them many are outdated (dj<=1.4), so asking a new quesiton. I guess this is simple, I'm much beginner with Django and Python.
I have some models, of which there are nested ManyToManyFields
,
I want to know, how can I save an object on the lower level while including all the top levels. That is just by adding the teacher object I want to add students and parents on the same fly.
My models
class Parent(models.Model):
name = models.CharField(max_length=10)
address = models.TextField(blank=True)
class Student(models.Model):
name = models.CharField(models.Model)
parents = models.ManyToManyField(Parent)
class Teacher(models.Model):
name = models.CharField(max_length=10)
student = models.ManyToManyField(Student)
I know to create an object to a simple model, by Teacher.objects.create(name='some name')
but don't know how to fill the student and parents.
sample data
teacher: Susan
students: Alan, Mike
parents: Alan-> George, Mike-> John Doe
(address be blank)
Thanks for any help.
pS. please don't say its impossible as it is possible through admin panel (GUI)