标注和提取到群组属性返回'sequence item 0'。

2024-09-29 17:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个查询,以便使用annotate和extract计算sqldb中不同月份的事件数

下面是使用的代码:

型号

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

有人能看出我的做法有什么不对吗? 对于这个查询,我遵循了documentation

我在Windows上使用Django 2.1.5和Python 3.7.0


Tags: djangoinselfdbsqlvenvmodelslib
1条回答
网友
1楼 · 发布于 2024-09-29 17:23:08

如果你看到ExtractMonth的文档,它是postgreSQL唯一的函数,如果你在MySQL数据库中使用postgreSQL函数,它当然会给出错误

我建议使用原始查询,或者将数据库更改为postgres

相关问题 更多 >

    热门问题