Enrolling in courses

After users create an account, they should be able to enroll in courses. In order to store enrollments, we need to create a many-to-many relationship between the Course and User models.

Edit the models.py file of the courses application and add the following field to the Course model:

students = models.ManyToManyField(User,                                  related_name='courses_joined',                                  blank=True)

From the shell, execute the following command to create a migration for this change:

python manage.py makemigrations

You will see an output similar to this:

Migrations for 'courses':  courses/migrations/0004_course_students.py    - Add field students to course

Then, execute the next command to apply pending migrations:

python manage.py migrate

You should see output ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.