5

I'm getting a weird error trying to select_related over multiple OneToOneField relationships, e.g. in the case where the target field is a grandchild subclass. I'd love someone to help me understand what's going on (or confirm that this is a bug in Django).

Illustration:

# models.py
from django.db import models

class A(models.Model):
    pass

class B(A):
    pass

class C(B):
    pass

Simple enough, right? Now I open the Django shell with a clean database:

>>> C().save()
>>> A.objects.select_related('b__c')
[]

Wait, what? Why is that queryset empty? A quick sanity check:

>>> A.objects.select_related('b')[0].b.c
<C: C object>

So why doesn't the select_related call work? Well, watch this:

>>> A.objects.select_related('b__c').__iter__().next()
... 
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/query.py", line 107, in _result_iter
    self._fill_cache()
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/query.py", line 772, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/query.py", line 273, in iterator
    for row in compiler.results_iter():
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 680, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 725, in execute_sql
    sql, params = self.as_sql()
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 58, in as_sql
    self.pre_sql_setup()
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 29, in pre_sql_setup
    self.fill_related_selections()
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 661, in fill_related_selections
    used, next, restricted, new_nullable)
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 617, in fill_related_selections
    chain = opts.get_base_chain(f.rel.to)
  File "/opt/webapps/asdf/lib/python2.6/site-packages/django/db/models/options.py", line 452, in get_base_chain
    % model._meta.module_name,)
TypeError: 'b' is not an ancestor of this model
>>> 

So, is this a Django bug, or am I not understanding something?

rfrankel
  • 675
  • 8
  • 20

2 Answers2

0

I added name=CharField to A and ran the following tests in shell:

>>> a = A(name='a_object')
>>> a.save()
>>> b = B(name='b_object')
>>> b.save()
>>> c = C(name='c_object')
>>> c.save()

>>> A.objects.all()
[<A: A object>, <A: A object>, <A: A object>]
>>> B.objects.all()
[<B: B object>, <B: B object>]
>>> C.objects.all()
[<C: C object>]

>>> A.objects.select_related('b__c')
[]
>>> A.objects.select_related('b__c').__iter__().next()
Traceback ....
    ...
    TypeError: 'b' is not an ancestor of this model

>>> d = A.objects.select_related('B__C')
>>> for item in d:
...     print item.name
... 
a_object
b_object
c_object

>>> test = A.objects.select_related('B__C').__iter__().next()
>>> test.name
u'a_object'

I know it's not an answer, and I don't know what to make of it. But basically I found the lowercase letter don't seem to mean anything without fields in the models.

j_syk
  • 6,511
  • 2
  • 39
  • 56
  • The lowercase letters are fields used for downcasting the object to a subclass instance. For example, A.objects.get(id=2).b will return the B object with id=2 if it exists, and raise an exception otherwise. Likewise, A.objects.get(id=3).b.c will return a C object (assuming it exists). The ticket I submitted was actually confirmed and accepted, so I do think this is a bug. – rfrankel Aug 11 '11 at 18:01
0

Got the same problem, but with quite a bit different model structure:

class A(models.Model):
    b = models.ForeignKey(B)
....
class B(models.Model):
....
class C(models.Model):
    b = models.OneToOneField(B)
    d = models.OneToOneField(D)
....
class D(models.Model):
....

So, when I write

A.objects.select_related('b__c__d').all()

I don't see all A - objects, only those where C is not null. However, when I write

A.objects.select_related('b__c').all()

Django ORM returns me all A objects, including those where C is null.

Seems like a bug to me, select_related should not work as a filter.

gleb.pitsevich
  • 1,269
  • 2
  • 12
  • 18