magic-removal: laid groundwork for subclassing.

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2432 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
cell 2006-02-28 00:50:06 +00:00
parent 36bfffccc4
commit 04b22e5002
2 changed files with 12 additions and 0 deletions

View File

@ -35,6 +35,12 @@ class ModelBase(type):
new_class.add_to_class('_meta', Options(attrs.pop('Meta', None))) new_class.add_to_class('_meta', Options(attrs.pop('Meta', None)))
new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {})) new_class.add_to_class('DoesNotExist', types.ClassType('DoesNotExist', (ObjectDoesNotExist,), {}))
# Build complete list of parents
for base in bases:
if '_meta' in dir(base):
new_class._meta.parents.append(base)
new_class._meta.parents.extend(base._meta.parents)
model_module = sys.modules[new_class.__module__] model_module = sys.modules[new_class.__module__]
if getattr(new_class._meta, 'app_label', None) is None: if getattr(new_class._meta, 'app_label', None) is None:
@ -46,6 +52,11 @@ class ModelBase(type):
for obj_name, obj in attrs.items(): for obj_name, obj in attrs.items():
new_class.add_to_class(obj_name, obj) new_class.add_to_class(obj_name, obj)
# Add Fields inherited from parents
for parent in new_class._meta.parents:
for field in parent._meta.fields:
field.contribute_to_class(new_class, field.name)
new_class._prepare() new_class._prepare()
register_models(new_class._meta.app_label, new_class) register_models(new_class._meta.app_label, new_class)

View File

@ -31,6 +31,7 @@ class Options:
self.pk = None self.pk = None
self.has_auto_field = False self.has_auto_field = False
self.one_to_one_field = None self.one_to_one_field = None
self.parents = []
def merge_meta(self): def merge_meta(self):
meta_attrs = self.meta.__dict__ meta_attrs = self.meta.__dict__