【问题标题】:Django Not implemented work aroundDjango 未实现的解决方法
【发布时间】:2014-01-25 19:09:03
【问题描述】:

所以this question之前被问过,但没有答案。我知道在 Django 中没有实现将带注释的查询集与不同的查询集相连接,但问题是:另一种方法是什么?

代码示例:

qs1 = Example.objects.filter(...).annotate(...)
qs2 = Example.objects.filter(...).distinct(...)
from itertools import chain
answer = chain(qs1,qs2)

但这将返回以下错误,因为在 Django 中“未实现”:

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/query.pyc in __iter__(self)
     94                - Responsible for turning the rows into model objects.
     95         """
---> 96         self._fetch_all()
     97         return iter(self._result_cache)
     98

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/query.pyc in _fetch_all(self)
    852     def _fetch_all(self):
    853         if self._result_cache is None:
--> 854             self._result_cache = list(self.iterator())
    855         if self._prefetch_related_lookups and not self._prefetch_done:
    856             self._prefetch_related_objects()

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/query.pyc in iterator(self)
    218             klass_info = get_klass_info(model, max_depth=max_depth,
    219                                         requested=requested, only_load=only_load)
--> 220         for row in compiler.results_iter():
    221             if fill_cache:
    222                 obj, _ = get_cached_row(row, index_start, db, klass_info,

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/sql/compiler.pyc in results_iter(self)
    708         fields = None
    709         has_aggregate_select = bool(self.query.aggregate_select)
--> 710         for rows in self.execute_sql(MULTI):
    711             for row in rows:
    712                 if has_aggregate_select:

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
    769         """
    770         try:
--> 771             sql, params = self.as_sql()
    772             if not sql:
    773                 raise EmptyResultSet

/Library/Python/2.7/site-packages/Django-1.6-py2.7.egg/django/db/models/sql/compiler.pyc in as_sql(self, with_limits, with_col_aliases)
    119             if distinct_fields:
    120                 raise NotImplementedError(
--> 121                     "annotate() + distinct(fields) not implemented.")
    122             if not ordering:
    123                 ordering = self.connection.ops.force_no_ordering()

NotImplementedError: annotate() + distinct(fields) not implemented.

所以,问题是:有什么方法可以完成链接这些查询集?

【问题讨论】:

    标签: python django django-queryset


    【解决方案1】:

    前段时间我不得不做这样的事情,所以你用 iter 工具做的事情是正确的,你必须将它强制转换为列表。

    from itertools import chain
    
    cars = Cars.objects.all()
    trucks = Truck.objects.all()
    all_vechiles = chain( list(cars), list(trucks) )
    

    来源:http://mushfiq.me/2013/08/04/django-merging-to-queryset-using-itertools/

    【讨论】:

    • 我仍然遇到同样的错误...请注意,您的示例没有使用 distinct 或 annotation,这就是导致我的错误的原因。如果我拿出我的distict(...),那么这在不使用list 的情况下可以工作,但我需要不同的调用
    • 这很奇怪,可能是因为 Django 查询很懒,你可能会尝试强制查询评估然后链接它们
    • 行得通!如果你编辑你的答案,我会接受它(只需将 list 函数放在 carstrucks 周围,而不是整个链)
    猜你喜欢
    • 2021-03-13
    • 1970-01-01
    • 2020-08-06
    • 2012-05-27
    • 2014-03-07
    • 1970-01-01
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多