/ / Django Nie zaimplementowano pracy - python, django, django-queryset

Django Nie zaimplementowano obejścia - python, django, django-queryset

Więc to pytanie został wcześniej zapytany, ale nie ma odpowiedzi. Wiem, że dołączenie zestawu zapytań z adnotacją do odrębnego nie jest zaimplementowane w Django, ale pytanie brzmi: jaki byłby alternatywny sposób, aby to zrobić?

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

ale zwróci następujący błąd, ponieważ „nie jest zaimplementowany” w 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.

Więc znowu pytanie brzmi: Jaki jest sposób na łączenie tych zestawów zapytań?

Odpowiedzi:

2 dla odpowiedzi № 1

Jakiś czas temu musiałem zrobić coś takiego, więc to, co robisz z tymi narzędziami, jest właściwe, będziesz musiał rzucić je na listę.

from itertools import chain

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

źródło tutaj:http://mushfiq.me/2013/08/04/django-merging-to-queryset-using-itertools/