我们正在尝试使用prefetch_related
. 它可以遵循GenericForeignKey
关系,并且可以更深入地了解,__
但不幸的是,如果相关模型没有这样的领域,它就会失败。
这是模型结构的一些示例
class ModelA(models.Model):
event_object = models.ForeignKey(SomeModelA)
class ModelB(models.Model):
event = models.ForeignKey(SomeModelB)
class ModelC(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey()
因此ModelC
instance 可以指向ModelA
或ModelB
。而且我可以使用这样的查询集来预取 A 和 B 模型:ModelC.objects.all().prefetch_related('content_object')
不幸的是,我还需要选择事件对象(SomeModelA
或SomeModelB
)
如果我尝试跑步
ModelC.objects.all().prefetch_related('content_object', 'content_object__event_object')
如果我只有ModelC
指向 的实例,它将起作用ModelA
,但在其他情况下,它将失败,因为ModelB
没有该event_object
字段event
而是有。
此模型在代码中的许多地方使用,因此重命名该字段不是一个好主意。所以我想知道是否有办法为字段/列创建别名。
我试图这样做:
class ModelB(models.Model):
event = models.ForeignKey(SomeModelB)
event_object = models.ForeignKey(SomeModelB, db_column='event_id', related_name='+')
使两个字段指向数据库表中的同一列。但是,这不起作用,因为它破坏了save
方法。Django 创建一个UPDATE
SQL 查询,其中一列放置两次并得到一个 DatabaseError
有没有办法创建这样的别名?或者也许有另一种解决方案可以prefetch_related
不抛出异常?
更新:在save
方法中有一个update_fields
参数可以用来排除这个字段。但是它是在 1.5 中引入的,我们使用的是 1.4。所以我继续寻找答案。
更新 #2:@shx2 要求我提供回溯。有 2 种可能的回溯。第一个 - 当第一个对象上缺少属性时:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 72, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 97, in __iter__
len(self)
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 89, in __len__
self._prefetch_related_objects()
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 570, in _prefetch_related_objects
prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 1664, in prefetch_related_objects
(attr, first_obj.__class__.__name__, lookup))
AttributeError: Cannot find 'event_object' on ModelB object, 'content_object__event_object' is an invalid parameter to prefetch_related()
如果 prefetch_related 参数对第一个对象有效,那么我得到第二个回溯:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 72, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 97, in __iter__
len(self)
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 89, in __len__
self._prefetch_related_objects()
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 570, in _prefetch_related_objects
prefetch_related_objects(self._result_cache, self._prefetch_related_lookups)
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 1680, in prefetch_related_objects
obj_list, additional_prl = prefetch_one_level(obj_list, prefetcher, attr)
File "/home/igor/workspace/projectname/eggs/Django-1.4.2-py2.7.egg/django/db/models/query.py", line 1803, in prefetch_one_level
qs = getattr(obj, attname).all()
AttributeError: 'ModelB' object has no attribute 'event_object'