Ticket #2363: tighter_subclasscheck.2.patch

File tighter_subclasscheck.2.patch, 1.8 KB (added by Chris.Wesseling@…, 8 years ago)

Merge of phil.h.smith@… cleaner code and my test.

  • tests/modeltests/invalid_models/models.py

     
    9797    m2m_3 = models.ManyToManyField('self', symmetrical=False)
    9898    m2m_4 = models.ManyToManyField('self', symmetrical=False)
    9999
     100class 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
     106class Car(models.Model):
     107    colour = models.CharField(maxlength=5)
     108    model = models.ForeignKey(Model)
     109
    100110model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "maxlength" attribute.
    101111invalid_models.fielderrors: "floatfield": FloatFields require a "decimal_places" attribute.
    102112invalid_models.fielderrors: "floatfield": FloatFields require a "max_digits" attribute.
  • django/db/models/base.py

     
    2222    "Metaclass for all models"
    2323    def __new__(cls, name, bases, attrs):
    2424        # 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
    2630            return super(ModelBase, cls).__new__(cls, name, bases, attrs)
    2731
    2832        # Create the class.
Back to Top