﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13781	select_related and multiple inheritance	Shaun Cutts	nobody	"I have a ""Profile"" model that inherits both from auth.User and Person -- representing people who are users.
Such people also have accounts -- Account has a OneToOneField relating it to a Profile.

A particular user has an account.


{{{
>>> profile = Profile.objects.get( pk = 9819 )
>>> profile
<Profile: bob test>
>>> profile.account
<Account: bob test>
}}}

----


But when I use a select_related, an error occurs:

{{{
In [36]: profile = Profile.objects.select_related( 'account' ).get( pk = 9819 )
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/shauncutts/<ipython console> in <module>()

/Users/shauncutts/dev/django/db/models/query.pyc in get(self, *args, **kwargs)
    334         if self.query.can_filter():
    335             clone = clone.order_by()
--> 336         num = len(clone)
    337         if num == 1:
    338             return clone._result_cache[0]

/Users/shauncutts/dev/django/db/models/query.pyc in __len__(self)
     79                 self._result_cache = list(self._iter)
     80             else:
---> 81                 self._result_cache = list(self.iterator())
     82         elif self._iter:
     83             self._result_cache.extend(list(self._iter))

/Users/shauncutts/dev/django/db/models/query.pyc in iterator(self)
    267 
    268         compiler = self.query.get_compiler(using=self.db)
--> 269         for row in compiler.results_iter():
    270             if fill_cache:
    271                 obj, _ = get_cached_row(self.model, row,

/Users/shauncutts/dev/django/db/models/sql/compiler.pyc in results_iter(self)
    670         resolve_columns = hasattr(self, 'resolve_columns')
    671         fields = None
--> 672         for rows in self.execute_sql(MULTI):
    673             for row in rows:
    674                 if resolve_columns:

/Users/shauncutts/dev/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    715         """"""
    716         try:
--> 717             sql, params = self.as_sql()
    718             if not sql:
    719                 raise EmptyResultSet

/Users/shauncutts/dev/django/db/models/sql/compiler.pyc in as_sql(self, with_limits, with_col_aliases)
     53         in the query.
     54         """"""
---> 55         self.pre_sql_setup()
     56         out_cols = self.get_columns(with_col_aliases)
     57         ordering, ordering_group_by = self.get_ordering()

/Users/shauncutts/dev/django/db/models/sql/compiler.pyc in pre_sql_setup(self)
     27             self.query.setup_inherited_models()
     28         if self.query.select_related and not self.query.related_select_cols:
---> 29             self.fill_related_selections()
     30 
     31     def quote_name_unless_alias(self, name):

/Users/shauncutts/dev/django/db/models/sql/compiler.pyc in fill_related_selections(self, opts, root_alias, cur_depth, used, requested, restricted, nullable, dupe_set, avoid_set)
    608                 alias = root_alias
    609                 alias_chain = []
--> 610                 chain = opts.get_base_chain(f.rel.to)
    611                 if chain is not None:
    612                     for int_model in chain:

/Users/shauncutts/dev/django/db/models/options.pyc in get_base_chain(self, model)
    427             return [model]
    428         for parent in self.parents:
--> 429             res = parent._meta.get_base_chain(model)
    430             if res:
    431                 res.insert(0, parent)

/Users/shauncutts/dev/django/db/models/options.pyc in get_base_chain(self, model)
    432                 return res
    433         raise TypeError('%r is not an ancestor of this model'
--> 434                 % model._meta.module_name)
    435 
    436     def get_parent_list(self):

TypeError: 'profile' is not an ancestor of this model
}}}

Examination of the stack trace shows that the problem may be related to the fact that Profile inherits from both User and Person

{{{
>>> pdb.pm()
> /Users/shauncutts/dev/django/db/models/options.py(434)get_base_chain()
-> % model._meta.module_name)
(Pdb) u
> /Users/shauncutts/dev/django/db/models/options.py(429)get_base_chain()
-> res = parent._meta.get_base_chain(model)
(Pdb) print parent
<class 'cranedata.db.base.models.Person'>
}}}
"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		shaun@… Vlastimil Zíma srmerritt mike@… gosia tomas.ehrlich@…	Ready for checkin	1	0	0	0	0	0
