我在尝试通过多个OneToOneField关系选择关联时遇到了一个奇怪的错误,例如在目标字段是孙子类的情况下。我希望有人帮助我了解正在发生的事情(或确认这是Django中的一个错误)。
插图:
# models.py
from django.db import models
class A(models.Model):
pass
class B(A):
pass
class C(B):
pass
够简单吧?现在我用一个干净的数据库打开Django shell:
>>> C().save()
>>> A.objects.select_related('b__c')
[]
等等,什么?为什么该查询集为空?快速健全检查:
>>> A.objects.select_related('b')[0].b.c
<C: C object>
那么为什么select_related调用不起作用?好吧,看这个:
>>> 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
>>>
那么,这是一个Django错误,还是我不理解某些东西?
答案 0 :(得分:0)
我将name = CharField添加到A并在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'
我知道这不是答案,我不知道该怎么做。但基本上我发现小写字母似乎并不代表模型中没有字段的任何内容。
答案 1 :(得分:0)
遇到了同样的问题,但模型结构有点不同:
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):
....
所以,当我写
A.objects.select_related('b__c__d').all()
我没有看到所有A - 对象,只有那些C不为空的对象。 但是,当我写
时A.objects.select_related('b__c').all()
Django ORM返回所有A对象,包括C为空的对象。
对我来说似乎是一个错误,select_related不应该用作过滤器。