From 31f11709e3b3a840b66a4822e6a7f83205a23dc7 Mon Sep 17 00:00:00 2001 From: bmedx Date: Tue, 14 Nov 2017 15:14:34 -0500 Subject: [PATCH] Change OpaqueKeyField's get_prep_lookup to a Lookup get_prep_lookup in a Field is deprecated in Django 1.11. This is the new way of doing this check. --- .../core/djangoapps/xmodule_django/models.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/openedx/core/djangoapps/xmodule_django/models.py b/openedx/core/djangoapps/xmodule_django/models.py index f9d12c8504..3a0fc6d58f 100644 --- a/openedx/core/djangoapps/xmodule_django/models.py +++ b/openedx/core/djangoapps/xmodule_django/models.py @@ -6,6 +6,7 @@ import warnings from django.core.exceptions import ValidationError from django.db import models +from django.db.models.lookups import IsNull from opaque_keys.edx.keys import BlockTypeKey, CourseKey, UsageKey from openedx.core.djangoapps.util.model_utils import CreatorMixin from xmodule.modulestore.django import modulestore @@ -116,16 +117,6 @@ class OpaqueKeyField(CreatorMixin, models.CharField): else: return value - def get_prep_lookup(self, lookup, value): - if lookup == 'isnull': - raise TypeError('Use {0}.Empty rather than None to query for a missing {0}'.format(self.__class__.__name__)) - - return super(OpaqueKeyField, self).get_prep_lookup( - lookup, - # strip key before comparing - _strip_value(value, lookup) - ) - def get_prep_value(self, value): if value is self.Empty or value is None: return '' # CharFields should use '' as their empty value, rather than None @@ -159,6 +150,19 @@ class OpaqueKeyField(CreatorMixin, models.CharField): return super(OpaqueKeyField, self).run_validators(value) +class OpaqueKeyFieldEmptyLookupIsNull(IsNull): + """ + This overrides the default __isnull model filter to help enforce the special way + we handle null / empty values in OpaqueKeyFields. + """ + def get_prep_lookup(self): + raise TypeError("Use this field's .Empty member rather than None or __isnull " + "to query for missing objects of this type.") + + +OpaqueKeyField.register_lookup(OpaqueKeyFieldEmptyLookupIsNull) + + class CourseKeyField(OpaqueKeyField): """ A django Field that stores a CourseKey object as a string.