From 55dde0286fc2aef9d2dc477c8193c9fb0e48cca3 Mon Sep 17 00:00:00 2001
From: Simon Charette <charette.s@gmail.com>
Date: Tue, 29 Jan 2013 00:28:09 -0500
Subject: [PATCH] Fixed #19688 -- Allow model subclassing with a custom
 metaclass using six.with_metaclass

---
 django/db/models/base.py        |    3 +++
 tests/modeltests/base/models.py |    5 +++++
 tests/modeltests/base/tests.py  |   36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 44 insertions(+)
 create mode 100644 tests/modeltests/base/__init__.py
 create mode 100644 tests/modeltests/base/models.py
 create mode 100644 tests/modeltests/base/tests.py

diff --git a/django/db/models/base.py b/django/db/models/base.py
index 38afc60..abb6a32 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -60,6 +60,9 @@ class ModelBase(type):
         super_new = super(ModelBase, cls).__new__
         # six.with_metaclass() inserts an extra class called 'NewBase' in the
         # inheritance tree: Model -> NewBase -> object. Ignore this class.
+        if (name == 'NewBase' and
+            any(isinstance(b, ModelBase) for b in bases)):
+            return super_new(cls, name, bases, attrs)
         parents = [b for b in bases if isinstance(b, ModelBase) and
                 not (b.__name__ == 'NewBase' and b.__mro__ == (b, object))]
         if not parents:
diff --git a/tests/modeltests/base/__init__.py b/tests/modeltests/base/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/modeltests/base/models.py b/tests/modeltests/base/models.py
new file mode 100644
index 0000000..bea762e
--- /dev/null
+++ b/tests/modeltests/base/models.py
@@ -0,0 +1,5 @@
+from django.db import models
+
+
+class CustomBaseModel(models.base.ModelBase):
+    pass
\ No newline at end of file
diff --git a/tests/modeltests/base/tests.py b/tests/modeltests/base/tests.py
new file mode 100644
index 0000000..d4d75f8
--- /dev/null
+++ b/tests/modeltests/base/tests.py
@@ -0,0 +1,36 @@
+from __future__ import unicode_literals
+
+from django.db import models
+from django.test.testcases import SimpleTestCase
+from django.utils import six
+from django.utils.unittest.case import skipIf
+
+from .models import CustomBaseModel
+
+
+class CustomBaseTest(SimpleTestCase):
+    @skipIf(six.PY3, 'test metaclass definition under Python 2')
+    def test_py2_custom_base(self):
+        """
+        Make sure models.Model can be subclassed with a valid custom base
+        using __metaclass__
+        """
+        try:
+            class PY2CustomModel(models.Model):
+                __metaclass__ = CustomBaseModel
+        except Exception:
+            self.fail("models.Model couldn't be subclassed with a valid "
+                      "custom base using __metaclass__.")
+
+    def test_six_custom_base(self):
+        """
+        Make sure models.Model can be subclassed with a valid custom base
+        using `six.with_metaclass`.
+        """
+        try:
+            class SixCustomModel(six.with_metaclass(CustomBaseModel,
+                                                 models.Model)):
+                pass
+        except Exception:
+            self.fail("models.Model couldn't be subclassed with a valid "
+                      "custom base using `six.with_metaclass`.")
\ No newline at end of file
-- 
1.7.9.5

