Ticket #8981: patch.diff

File patch.diff, 6.4 KB (added by Andrew Gibson, 16 years ago)

comments

  • trunk/django/db/models/base.py

     
    3333            # If this isn't a subclass of Model, don't do anything special.
    3434            return super_new(cls, name, bases, attrs)
    3535
    36         # Create the class.
     36        # extract the __module__ value for creating the new class
     37        # also used for creating individual subclasses of
     38        # ObjectDoesNotExist, MultipleObjectsReturned
    3739        module = attrs.pop('__module__')
     40        # Create the class.       
     41        # notice that each attrs is ignored except for __module__
     42        # other attrs are added later
    3843        new_class = super_new(cls, name, bases, {'__module__': module})
     44        # get reference to enclosed Meta class, and determine if this class was
     45        # decalred as abstract
     46        # if there is no Meta class, attr_meta = None
    3947        attr_meta = attrs.pop('Meta', None)
    4048        abstract = getattr(attr_meta, 'abstract', False)
     49        # if there isn't a Meta class, try and find one from an ancestor class
    4150        if not attr_meta:
    4251            meta = getattr(new_class, 'Meta', None)
    4352        else:
    4453            meta = attr_meta
     54        # meta is now the reference to a Meta class, if any
     55        # retrive an inherited _meta, if any
    4556        base_meta = getattr(new_class, '_meta', None)
    46 
     57       
     58        # attempt to set the app_label value
    4759        if getattr(meta, 'app_label', None) is None:
    4860            # Figure out the app_label by looking one level up.
    4961            # For 'django.contrib.sites.models', this would be 'sites'.
     
    5163            kwargs = {"app_label": model_module.__name__.split('.')[-2]}
    5264        else:
    5365            kwargs = {}
    54 
     66       
     67        #attach the _meta attribute, an instance of Options
    5568        new_class.add_to_class('_meta', Options(meta, **kwargs))
     69        # setup non-abstract classes
    5670        if not abstract:
     71          # add error classes as attributes into class we're constructing unless it's abstract
    5772            new_class.add_to_class('DoesNotExist',
    5873                    subclass_exception('DoesNotExist', ObjectDoesNotExist, module))
    5974            new_class.add_to_class('MultipleObjectsReturned',
    6075                    subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module))
    6176            if base_meta and not base_meta.abstract:
    62                 # Non-abstract child classes inherit some attributes from their
     77                # Non-abstract child classes inherit some _meta attributes from their
    6378                # non-abstract parent (unless an ABC comes before it in the
    6479                # method resolution order).
    6580                if not hasattr(meta, 'ordering'):
     
    6782                if not hasattr(meta, 'get_latest_by'):
    6883                    new_class._meta.get_latest_by = base_meta.get_latest_by
    6984
     85        # determine if there is an inherited default_manager, if not explicity set it to None
    7086        if getattr(new_class, '_default_manager', None):
    7187            new_class._default_manager = None
    7288
    7389        # Bail out early if we have already created this class.
     90        # all models are registered in a Borg pattern class
    7491        m = get_model(new_class._meta.app_label, name, False)
    7592        if m is not None:
    7693            return m
    7794
    7895        # Add all attributes to the class.
     96        # since different attributes need to be added in different ways, they weren't
     97        # included in the namespace attribute for the type.__new__() call
     98        # add_to_class will either attach them normally (setattr) or use the attribute's
     99        # add_to_class method, if it exists
    79100        for obj_name, obj in attrs.items():
    80101            new_class.add_to_class(obj_name, obj)
    81102
     
    94115                         new_class._meta.virtual_fields
    95116            field_names = set([f.name for f in new_fields])
    96117
     118            # for multi-table inheritance, add a one-to-one relation between
     119            # the child class (the class curently being constructed) and the parent class
    97120            if not base._meta.abstract:
    98121                # Concrete classes...
    99122                if base in o2o_map:
     
    159182        # registered version.
    160183        return get_model(new_class._meta.app_label, name, False)
    161184
     185    # method for adding attributes to the class being constructed
    162186    def add_to_class(cls, name, value):
    163187        if hasattr(value, 'contribute_to_class'):
    164188            value.contribute_to_class(cls, name)
  • trunk/django/db/models/options.py

     
    2323                 'order_with_respect_to', 'app_label', 'db_tablespace',
    2424                 'abstract')
    2525
     26# instance of this class becomes somemodel._meta
     27# this class' instances are attached while the model class is being
     28# constructued and before the model's __init__() is run
    2629class Options(object):
    2730    def __init__(self, meta, app_label=None):
    2831        self.local_fields, self.local_many_to_many = [], []
     
    4952        # are passed onto any children.
    5053        self.abstract_managers = []
    5154
     55    # method for adding instance of Options to a model
    5256    def contribute_to_class(self, cls, name):
    5357        from django.db import connection
    5458        from django.db.backends.util import truncate_name
    5559
    5660        cls._meta = self
     61        # installed is boolean, details whether this model makes up part of an
     62        # installed application
    5763        self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS
    5864        # First, construct the default values for these options.
    5965        self.object_name = cls.__name__
  • trunk/django/db/models/fields/__init__.py

     
    154154        if self.verbose_name is None and name:
    155155            self.verbose_name = name.replace('_', ' ')
    156156
     157    # method for adding a field to a model class
     158    # faily general purposes, only subclassed a couple times
    157159    def contribute_to_class(self, cls, name):
    158160        self.set_attributes_from_name(name)
    159161        cls._meta.add_field(self)
Back to Top