diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-04 07:20:08 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-04 07:20:08 +0000 |
| commit | dfd7a6c7811c49371838f593387ae6db7bb7ef30 (patch) | |
| tree | debb0261d2fda060c4c0a5cd98edb0dd4e09d422 /django | |
| parent | 0e93f60c7f6de821e31424b0e7c26586155a7a1a (diff) | |
Fixed #10251 -- Fixed model inheritance when there's also an explicit pk field.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9970 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/options.py | 15 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 8 |
2 files changed, 19 insertions, 4 deletions
diff --git a/django/db/models/options.py b/django/db/models/options.py index 9e1bfb4ce1..06e17c61ad 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -435,6 +435,21 @@ class Options(object): result.update(parent._meta.get_parent_list()) return result + def get_ancestor_link(self, ancestor): + """ + Returns the field on the current model which points to the given + "ancestor". This is possible an indirect link (a pointer to a parent + model, which points, eventually, to the ancestor). Used when + constructing table joins for model inheritance. + + Returns None if the model isn't an ancestor of this one. + """ + if ancestor in self.parents: + return self.parents[ancestor] + for parent in self.parents: + if parent._meta.get_ancestor_link(ancestor): + return self.parents[parent] + def get_ordered_objects(self): "Returns a list of Options objects that are ordered with respect to this object." if not hasattr(self, '_ordered_objects'): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 80bbfc401a..fb07529ee2 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -643,14 +643,14 @@ class BaseQuery(object): aliases = set() if start_alias: seen = {None: start_alias} - root_pk = opts.pk.column for field, model in opts.get_fields_with_model(): if start_alias: try: alias = seen[model] except KeyError: + link_field = opts.get_ancestor_link(model) alias = self.join((start_alias, model._meta.db_table, - root_pk, model._meta.pk.column)) + link_field.column, model._meta.pk.column)) seen[model] = alias else: # If we're starting from the base model of the queryset, the @@ -1156,13 +1156,13 @@ class BaseQuery(object): as_sql()). """ opts = self.model._meta - root_pk = opts.pk.column root_alias = self.tables[0] seen = {None: root_alias} for field, model in opts.get_fields_with_model(): if model not in seen: + link_field = opts.get_ancestor_link(model) seen[model] = self.join((root_alias, model._meta.db_table, - root_pk, model._meta.pk.column)) + link_field.column, model._meta.pk.column)) self.included_inherited_models = seen def remove_inherited_models(self): |
