From 6f524ce2239ee40eac41b066adc9c4dc3febe542 Mon Sep 17 00:00:00 2001 From: Guruprasad Lakshmi Narayanan Date: Fri, 19 Jul 2019 18:43:17 +0530 Subject: [PATCH] Use a callable as the default value for CourseEnrollment.mode The previous behaviour of using a variable causes Django to complain about changes not reflected in a migration when the default course mode slug is changed. --- common/djangoapps/course_modes/models.py | 8 ++++++ ..._courseenrollment_mode_callable_default.py | 26 +++++++++++++++++++ common/djangoapps/student/models.py | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 common/djangoapps/student/migrations/0027_courseenrollment_mode_callable_default.py diff --git a/common/djangoapps/course_modes/models.py b/common/djangoapps/course_modes/models.py index e967fe3f16..0bd8a1f6d6 100644 --- a/common/djangoapps/course_modes/models.py +++ b/common/djangoapps/course_modes/models.py @@ -255,6 +255,14 @@ class CourseMode(models.Model): self.expiration_datetime_is_explicit = True self._expiration_datetime = new_datetime + @classmethod + def get_default_mode_slug(cls): + """ + Returns the default mode slug to be used in the CourseEnrollment model mode field + as the default value. + """ + return cls.DEFAULT_MODE_SLUG + @classmethod def all_modes_for_courses(cls, course_id_list): """Find all modes for a list of course IDs, including expired modes. diff --git a/common/djangoapps/student/migrations/0027_courseenrollment_mode_callable_default.py b/common/djangoapps/student/migrations/0027_courseenrollment_mode_callable_default.py new file mode 100644 index 0000000000..77caab9e59 --- /dev/null +++ b/common/djangoapps/student/migrations/0027_courseenrollment_mode_callable_default.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.22 on 2019-07-19 13:06 +from __future__ import unicode_literals + +import course_modes.models +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('student', '0026_allowedauthuser'), + ] + + operations = [ + migrations.AlterField( + model_name='courseenrollment', + name='mode', + field=models.CharField(default=course_modes.models.CourseMode.get_default_mode_slug, max_length=100), + ), + migrations.AlterField( + model_name='historicalcourseenrollment', + name='mode', + field=models.CharField(default=course_modes.models.CourseMode.get_default_mode_slug, max_length=100), + ), + ] diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 58bd592bb0..1795d1f127 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -1101,7 +1101,7 @@ class CourseEnrollment(models.Model): # Represents the modes that are possible. We'll update this later with a # list of possible values. - mode = models.CharField(default=CourseMode.DEFAULT_MODE_SLUG, max_length=100) + mode = models.CharField(default=CourseMode.get_default_mode_slug, max_length=100) # An audit row will be created for every change to a CourseEnrollment. This # will create a new model behind the scenes - HistoricalCourseEnrollment and a