Skip to content

Commit ae4125f

Browse files
committed
Removed a Python 3-compatibility hack.
Thanks Preston Holmes for the patch.
1 parent ebc89a8 commit ae4125f

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

django/db/models/base.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ class ModelBase(type):
3434
"""
3535
def __new__(cls, name, bases, attrs):
3636
super_new = super(ModelBase, cls).__new__
37-
parents = [b for b in bases if isinstance(b, ModelBase)]
37+
# six.with_metaclass() inserts an extra class called 'NewBase' in the
38+
# inheritance tree: Model -> NewBase -> object. Ignore this class.
39+
parents = [b for b in bases if isinstance(b, ModelBase) and
40+
not (b.__name__ == 'NewBase' and b.__mro__ == (b, object))]
3841
if not parents:
3942
# If this isn't a subclass of Model, don't do anything special.
4043
return super_new(cls, name, bases, attrs)
@@ -276,8 +279,7 @@ def __init__(self, db=None):
276279
# This impacts validation only; it has no effect on the actual save.
277280
self.adding = True
278281

279-
280-
class ModelWithoutMeta(object):
282+
class Model(six.with_metaclass(ModelBase, object)):
281283
_deferred = False
282284

283285
def __init__(self, *args, **kwargs):
@@ -370,7 +372,7 @@ def __init__(self, *args, **kwargs):
370372
pass
371373
if kwargs:
372374
raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
373-
super(ModelWithoutMeta, self).__init__()
375+
super(Model, self).__init__()
374376
signals.post_init.send(sender=self.__class__, instance=self)
375377

376378
def __repr__(self):
@@ -402,7 +404,7 @@ def __reduce__(self):
402404
only module-level classes can be pickled by the default path.
403405
"""
404406
if not self._deferred:
405-
return super(ModelWithoutMeta, self).__reduce__()
407+
return super(Model, self).__reduce__()
406408
data = self.__dict__
407409
defers = []
408410
for field in self._meta.fields:
@@ -877,14 +879,6 @@ def clean_fields(self, exclude=None):
877879
raise ValidationError(errors)
878880

879881

880-
# For unknown reasons, six.with_metaclass doesn't work correctly for Model.
881-
# Fallback to exec'ing the appropriate syntax for each Python version.
882-
883-
if six.PY3:
884-
six.exec_("class Model(ModelWithoutMeta, metaclass=ModelBase): pass")
885-
else:
886-
six.exec_("class Model(ModelWithoutMeta): __metaclass__ = ModelBase")
887-
888882

889883
############################################
890884
# HELPER FUNCTIONS (CURRIED MODEL METHODS) #

0 commit comments

Comments
 (0)