diff -r 1a5661fe179d django/db/models/base.py --- a/django/db/models/base.py Tue Nov 16 15:14:04 2010 +0000 +++ b/django/db/models/base.py Tue Nov 16 23:30:27 2010 +0100 @@ -126,13 +126,14 @@ o2o_map = dict([(f.rel.to, f) for f in new_class._meta.local_fields if isinstance(f, OneToOneField)]) - for base in parents: + for creation_level, base in enumerate(parents): original_base = base if not hasattr(base, '_meta'): # Things without _meta aren't functional models, so they're # uninteresting parents. continue + new_class._meta.creation_level = creation_level + 1 parent_fields = base._meta.local_fields + base._meta.local_many_to_many # Check for clashes between locally declared fields and those # on the base classes (we cannot handle shadowed fields at the diff -r 1a5661fe179d django/db/models/fields/__init__.py --- a/django/db/models/fields/__init__.py Tue Nov 16 15:14:04 2010 +0000 +++ b/django/db/models/fields/__init__.py Tue Nov 16 23:30:27 2010 +0100 @@ -57,6 +57,7 @@ # The auto_creation_counter is used for fields that Django implicitly # creates, creation_counter is used for all user-specified fields. creation_counter = 0 + creation_level = 0 auto_creation_counter = -1 default_validators = [] # Default set of validators default_error_messages = { @@ -121,7 +122,9 @@ def __cmp__(self, other): # This is needed because bisect does not take a comparison function. - return cmp(self.creation_counter, other.creation_counter) + if self.creation_counter <= -1 or other.creation_counter <= -1: + return cmp(self.creation_counter, other.creation_counter) + return cmp((self.creation_level, self.creation_counter), (other.creation_level, other.creation_counter)) def __deepcopy__(self, memodict): # We don't have to deepcopy very much here, since most things are not diff -r 1a5661fe179d django/db/models/options.py --- a/django/db/models/options.py Tue Nov 16 15:14:04 2010 +0000 +++ b/django/db/models/options.py Tue Nov 16 23:30:27 2010 +0100 @@ -27,6 +27,7 @@ class Options(object): def __init__(self, meta, app_label=None): self.local_fields, self.local_many_to_many = [], [] + self.creation_level = 0 self.virtual_fields = [] self.module_name, self.verbose_name = None, None self.verbose_name_plural = None @@ -148,6 +149,7 @@ # the "creation_counter" attribute of the field. # Move many-to-many related fields from self.fields into # self.many_to_many. + field.creation_level = self.creation_level if field.rel and isinstance(field.rel, ManyToManyRel): self.local_many_to_many.insert(bisect(self.local_many_to_many, field), field) if hasattr(self, '_m2m_cache'):