Я пытаюсь создать запрос для подсчета вхождений за отдельные месяцы в моей базе данных SQL, используя для этого аннотации и извлечения.
Вот код, используемый:
Модель
class Results(model.Model):
trip = models.BooleanField(default=False)
created_on = models.DateTimeField(default=datetime.now)
Запрос
from django.db.models.functions import Extract, ExtractYear
from django.db.models import Count
res = Results.objects.annotate(month=ExtractMonth('created_on'))
.values('month').annotate(count=Count('month'))
OR
res = Results.objects.annotate(month=Extract('created_on','month'))
.values('month').annotate(count=Count('month'))
Оба возвращают:
Ошибка
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\query.py", line 244, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\query.py", line 268, in __iter__
self._fetch_all()
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\query.py", line 106, in __iter__
for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1017, in results_iter
results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1052, in execute_sql
sql, params = self.as_sql()
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 449, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 50, in pre_sql_setup
self.setup_query()
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 41, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 244, in get_select
sql, params = self.compile(col, select_format=True)
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\sql\compiler.py", line 390, in compile
sql, params = node.as_sql(self, self.connection)
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\aggregates.py", line 76, in as_sql
return super().as_sql(compiler, connection, **extra_context)
File "C:\Users\VIBRITO\Desktop\Projetos\webapp\venv\lib\site-packages\django\db\models\expressions.py", line 618, in as_sql
data['expressions'] = data['field'] = arg_joiner.join(sql_parts)
TypeError: sequence item 0: expected str instance, tuple found
Кто-нибудь может увидеть, что не так в моем подходе?Я следовал документации для этого запроса.
Я использую Django 2.1.5 и Python 3.7.0 для Windows.