|
24 | 24 | from django.utils.translation import ugettext_lazy as _ |
25 | 25 | from django.utils.functional import curry |
26 | 26 | from django.utils.encoding import smart_str, force_unicode |
| 27 | +from django.utils import six |
27 | 28 | from django.utils.text import get_text_list, capfirst |
28 | 29 |
|
29 | 30 |
|
@@ -275,8 +276,8 @@ def __init__(self, db=None): |
275 | 276 | # This impacts validation only; it has no effect on the actual save. |
276 | 277 | self.adding = True |
277 | 278 |
|
278 | | -class Model(object): |
279 | | - __metaclass__ = ModelBase |
| 279 | + |
| 280 | +class ModelWithoutMeta(object): |
280 | 281 | _deferred = False |
281 | 282 |
|
282 | 283 | def __init__(self, *args, **kwargs): |
@@ -369,7 +370,7 @@ def __init__(self, *args, **kwargs): |
369 | 370 | pass |
370 | 371 | if kwargs: |
371 | 372 | raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0]) |
372 | | - super(Model, self).__init__() |
| 373 | + super(ModelWithoutMeta, self).__init__() |
373 | 374 | signals.post_init.send(sender=self.__class__, instance=self) |
374 | 375 |
|
375 | 376 | def __repr__(self): |
@@ -401,7 +402,7 @@ def __reduce__(self): |
401 | 402 | only module-level classes can be pickled by the default path. |
402 | 403 | """ |
403 | 404 | if not self._deferred: |
404 | | - return super(Model, self).__reduce__() |
| 405 | + return super(ModelWithoutMeta, self).__reduce__() |
405 | 406 | data = self.__dict__ |
406 | 407 | defers = [] |
407 | 408 | for field in self._meta.fields: |
@@ -876,6 +877,15 @@ def clean_fields(self, exclude=None): |
876 | 877 | raise ValidationError(errors) |
877 | 878 |
|
878 | 879 |
|
| 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 | + |
| 888 | + |
879 | 889 | ############################################ |
880 | 890 | # HELPER FUNCTIONS (CURRIED MODEL METHODS) # |
881 | 891 | ############################################ |
|
0 commit comments