From 4b0fcefbc28694876f7fffc14a5584f84f898a21 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Wed, 8 Mar 2006 20:59:00 +0000 Subject: [PATCH] magic-removal: model inheirtance can now be more than 1 level deep. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@2506 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 8 ++++++-- tests/modeltests/model_inheritance/models.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index fd29a3eaf4..19dce10697 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -1,6 +1,6 @@ import django.db.models.manipulators import django.db.models.manager -from django.db.models.fields import AutoField, ImageField +from django.db.models.fields import AutoField, ImageField, FieldDoesNotExist from django.db.models.fields.related import OneToOne, ManyToOne from django.db.models.related import RelatedObject from django.db.models.query import orderlist2sql, delete_objects @@ -56,7 +56,11 @@ class ModelBase(type): # 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) + # Only add parent fields if they aren't defined for this class. + try: + new_class._meta.get_field(field.name) + except FieldDoesNotExist: + field.contribute_to_class(new_class, field.name) new_class._prepare() diff --git a/tests/modeltests/model_inheritance/models.py b/tests/modeltests/model_inheritance/models.py index 0f9dd19f5d..27e1df9c01 100644 --- a/tests/modeltests/model_inheritance/models.py +++ b/tests/modeltests/model_inheritance/models.py @@ -19,9 +19,19 @@ class Restaurant(Place): def __repr__(self): return "%s the restaurant" % self.name +class ItalianRestaurant(Restaurant): + serves_gnocchi = models.BooleanField() + + def __repr__(self): + return "%s the italian restaurant" % self.name + API_TESTS = """ # Make sure Restaurant has the right fields in the right order. >>> [f.name for f in Restaurant._meta.fields] ['id', 'name', 'address', 'serves_hot_dogs', 'serves_pizza'] +# Make sure ItalianRestaurant has the right fields in the right order. +>>> [f.name for f in ItalianRestaurant._meta.fields] +['id', 'name', 'address', 'serves_hot_dogs', 'serves_pizza', 'serves_gnocchi'] + """