11

我们正在尝试使用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()

因此ModelCinstance 可以指向ModelAModelB。而且我可以使用这样的查询集来预取 A 和 B 模型:ModelC.objects.all().prefetch_related('content_object')不幸的是,我还需要选择事件对象(SomeModelASomeModelB

如果我尝试跑步

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 创建一个UPDATESQL 查询,其中一列放置两次并得到一个 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'
4

1 回答 1

2

它看起来像 django 中的错误或疏忽。作为一种解决方法,您可以尝试定义一个执行 2 阶段预取的自定义管理器。

from django.db import models
from django.db.models import Q
from django.contrib.contenttypes.models import ContentType

class PrefetchWorkaroundManager(models.Manager):
    def get_queryset(self):
        q = super(PrefetchWorkaroundManager, self).get_queryset()
        content_typeA = ContentType.objects.get_for_model(ModelA)
        content_typeB = ContentType.objects.get_for_model(ModelB)
        return q.filter(content_type__pk = content_typeA.id).prefetch_related('content_object', 'content_object__event_object') | \
               q.filter(content_type__pk = content_typeB.id).prefetch_related('content_object', 'content_object__event')

class ModelC(models.Model):
    ...

    objects_prefetched = PrefetchWorkaroundManager()

每个想要进行预取的调用者都应该访问ModelC.objects_prefetched而不是ModelC.objects

ModelC.objects_prefetched.filter(...)

我承认,我没有测试它,所以它可能无法按原样工作。但我相信这种方法是合理的。

于 2013-03-19T20:08:28.330 回答