Here's how I did it:
class Student(models.Model): name = models.CharField(max_length=50) project = models.ManyToManyField(Course)
My models are a little different because I started to code up a solution when you first sent me a message. But hopefully it makes sense.
In shell:
>>> from courses.models import Course >>> from accounts.models import Student >>> course = Course(name='Django', num_of_students=2) >>> course.save() >>> student = Student(name='Carla') >>> student.project.add(course)
then you can save.
If you try to add a course or project before saving the student first, you'll get
ValueError: 'Student' instance needs to have a primary key value before a many-to-many relationship can be used.
Because the student to project/course relationship (models.ManyToManyField) can't be created without the keys.
For more information on relationships: https://docs.djangoproject.com/en/dev/ref/models/relations/
I realize it was just a simple testing exercise but you probably don't really want to store the number of students right that. Probably a count of the number of objects in the relationship would be better? So you could have -- and I think this was in your original message -- on project 'students' that are manytomany to your student model. Then for your view or such, you can show a count of all the students in that course.