Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2363 -- Improved base class checking in ModelBase metaclass. T…

…hanks to

combined work from phil.h.smith@gmail.com and Chris.Wesseling@cwi.nl.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@4881 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1109e722aaf1c9100bcb4ea0c3e23b5fd4c8c8de 1 parent 04c155f
Malcolm Tredinnick authored March 31, 2007
6  django/db/models/base.py
@@ -22,7 +22,11 @@ class ModelBase(type):
22 22
     "Metaclass for all models"
23 23
     def __new__(cls, name, bases, attrs):
24 24
         # If this isn't a subclass of Model, don't do anything special.
25  
-        if name == 'Model' or not filter(lambda b: issubclass(b, Model), bases):
  25
+        try:
  26
+            if not filter(lambda b: issubclass(b, Model), bases):
  27
+                return super(ModelBase, cls).__new__(cls, name, bases, attrs)
  28
+        except NameError:
  29
+            # Model isn't defined yet, meaning we're looking at django's own Model defined below 
26 30
             return super(ModelBase, cls).__new__(cls, name, bases, attrs)
27 31
 
28 32
         # Create the class.
10  tests/modeltests/invalid_models/models.py
@@ -97,6 +97,16 @@ class SelfClashM2M(models.Model):
97 97
     m2m_3 = models.ManyToManyField('self', symmetrical=False)
98 98
     m2m_4 = models.ManyToManyField('self', symmetrical=False)
99 99
 
  100
+class Model(models.Model):
  101
+    "But it's valid to call a model Model."
  102
+    year = models.PositiveIntegerField() #1960
  103
+    make = models.CharField(maxlength=10) #Aston Martin
  104
+    name = models.CharField(maxlength=10) #DB 4 GT
  105
+
  106
+class Car(models.Model):
  107
+    colour = models.CharField(maxlength=5)
  108
+    model = models.ForeignKey(Model)
  109
+
100 110
 model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "maxlength" attribute.
101 111
 invalid_models.fielderrors: "floatfield": FloatFields require a "decimal_places" attribute.
102 112
 invalid_models.fielderrors: "floatfield": FloatFields require a "max_digits" attribute.

0 notes on commit 1109e72

Please sign in to comment.
Something went wrong with that request. Please try again.