Remove SubfieldBase metaclass

This commit is contained in:
Brian Beggs
2017-10-23 14:08:08 -04:00
parent c12923838f
commit ca92ffe21d
9 changed files with 42 additions and 18 deletions

View File

@@ -551,10 +551,7 @@ class CourseOverview(TimeStampedModel):
"""
Returns all course keys from course overviews.
"""
return [
CourseKey.from_string(course_overview['id'])
for course_overview in CourseOverview.objects.values('id')
]
return CourseOverview.objects.values_list('id', flat=True)
def is_discussion_tab_enabled(self):
"""

View File

@@ -0,0 +1,27 @@
class Creator(object):
"""
A placeholder class that provides a way to set the attribute on the model.
"""
def __init__(self, field):
self.field = field
def __get__(self, obj, type=None):
if obj is None:
return self
return obj.__dict__[self.field.name]
def __set__(self, obj, value):
obj.__dict__[self.field.name] = self.field.to_python(value)
class CreatorMixin(object):
"""
Mixin class to provide SubfieldBase functionality to django fields.
See: https://docs.djangoproject.com/en/1.11/releases/1.8/#subfieldbase
"""
def contribute_to_class(self, cls, name, *args, **kwargs):
super(CreatorMixin, self).contribute_to_class(cls, name, *args, **kwargs)
setattr(cls, name, Creator(self))
def from_db_value(self, value, expression, connection, context):
return self.to_python(value)

View File

@@ -7,7 +7,7 @@ import warnings
from django.core.exceptions import ValidationError
from django.db import models
from opaque_keys.edx.keys import BlockTypeKey, CourseKey, UsageKey
from openedx.core.djangoapps.util.model_utils import CreatorMixin
from xmodule.modulestore.django import modulestore
log = logging.getLogger(__name__)
@@ -68,7 +68,7 @@ def _strip_value(value, lookup='exact'):
return stripped_value
class OpaqueKeyField(models.CharField):
class OpaqueKeyField(CreatorMixin, models.CharField):
"""
A django field for storing OpaqueKeys.
@@ -81,8 +81,6 @@ class OpaqueKeyField(models.CharField):
"""
description = "An OpaqueKey object, saved to the DB in the form of a string."
__metaclass__ = models.SubfieldBase
Empty = object()
KEY_CLASS = None