Ticket #8981: patch.diff
File patch.diff, 6.4 KB (added by , 16 years ago) |
---|
-
trunk/django/db/models/base.py
33 33 # If this isn't a subclass of Model, don't do anything special. 34 34 return super_new(cls, name, bases, attrs) 35 35 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 37 39 module = attrs.pop('__module__') 40 # Create the class. 41 # notice that each attrs is ignored except for __module__ 42 # other attrs are added later 38 43 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 39 47 attr_meta = attrs.pop('Meta', None) 40 48 abstract = getattr(attr_meta, 'abstract', False) 49 # if there isn't a Meta class, try and find one from an ancestor class 41 50 if not attr_meta: 42 51 meta = getattr(new_class, 'Meta', None) 43 52 else: 44 53 meta = attr_meta 54 # meta is now the reference to a Meta class, if any 55 # retrive an inherited _meta, if any 45 56 base_meta = getattr(new_class, '_meta', None) 46 57 58 # attempt to set the app_label value 47 59 if getattr(meta, 'app_label', None) is None: 48 60 # Figure out the app_label by looking one level up. 49 61 # For 'django.contrib.sites.models', this would be 'sites'. … … 51 63 kwargs = {"app_label": model_module.__name__.split('.')[-2]} 52 64 else: 53 65 kwargs = {} 54 66 67 #attach the _meta attribute, an instance of Options 55 68 new_class.add_to_class('_meta', Options(meta, **kwargs)) 69 # setup non-abstract classes 56 70 if not abstract: 71 # add error classes as attributes into class we're constructing unless it's abstract 57 72 new_class.add_to_class('DoesNotExist', 58 73 subclass_exception('DoesNotExist', ObjectDoesNotExist, module)) 59 74 new_class.add_to_class('MultipleObjectsReturned', 60 75 subclass_exception('MultipleObjectsReturned', MultipleObjectsReturned, module)) 61 76 if base_meta and not base_meta.abstract: 62 # Non-abstract child classes inherit some attributes from their77 # Non-abstract child classes inherit some _meta attributes from their 63 78 # non-abstract parent (unless an ABC comes before it in the 64 79 # method resolution order). 65 80 if not hasattr(meta, 'ordering'): … … 67 82 if not hasattr(meta, 'get_latest_by'): 68 83 new_class._meta.get_latest_by = base_meta.get_latest_by 69 84 85 # determine if there is an inherited default_manager, if not explicity set it to None 70 86 if getattr(new_class, '_default_manager', None): 71 87 new_class._default_manager = None 72 88 73 89 # Bail out early if we have already created this class. 90 # all models are registered in a Borg pattern class 74 91 m = get_model(new_class._meta.app_label, name, False) 75 92 if m is not None: 76 93 return m 77 94 78 95 # 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 79 100 for obj_name, obj in attrs.items(): 80 101 new_class.add_to_class(obj_name, obj) 81 102 … … 94 115 new_class._meta.virtual_fields 95 116 field_names = set([f.name for f in new_fields]) 96 117 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 97 120 if not base._meta.abstract: 98 121 # Concrete classes... 99 122 if base in o2o_map: … … 159 182 # registered version. 160 183 return get_model(new_class._meta.app_label, name, False) 161 184 185 # method for adding attributes to the class being constructed 162 186 def add_to_class(cls, name, value): 163 187 if hasattr(value, 'contribute_to_class'): 164 188 value.contribute_to_class(cls, name) -
trunk/django/db/models/options.py
23 23 'order_with_respect_to', 'app_label', 'db_tablespace', 24 24 'abstract') 25 25 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 26 29 class Options(object): 27 30 def __init__(self, meta, app_label=None): 28 31 self.local_fields, self.local_many_to_many = [], [] … … 49 52 # are passed onto any children. 50 53 self.abstract_managers = [] 51 54 55 # method for adding instance of Options to a model 52 56 def contribute_to_class(self, cls, name): 53 57 from django.db import connection 54 58 from django.db.backends.util import truncate_name 55 59 56 60 cls._meta = self 61 # installed is boolean, details whether this model makes up part of an 62 # installed application 57 63 self.installed = re.sub('\.models$', '', cls.__module__) in settings.INSTALLED_APPS 58 64 # First, construct the default values for these options. 59 65 self.object_name = cls.__name__ -
trunk/django/db/models/fields/__init__.py
154 154 if self.verbose_name is None and name: 155 155 self.verbose_name = name.replace('_', ' ') 156 156 157 # method for adding a field to a model class 158 # faily general purposes, only subclassed a couple times 157 159 def contribute_to_class(self, cls, name): 158 160 self.set_attributes_from_name(name) 159 161 cls._meta.add_field(self)