注释和提取组属性 returns 'Sequence Item 0'
Annotate and Extract to Group Attribute returns 'Sequence Item 0'
我正在尝试构建一个查询来计算我的 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'))
两个return:
错误
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
如果您看到 ExtractMonth 的文档,它只是 postgreSQL 的函数,如果您将 postgreSQL 函数与 MySQL 数据库一起使用,当然会出错。
我建议使用原始查询,或将数据库更改为 postgres。
我正在尝试构建一个查询来计算我的 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'))
两个return:
错误
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如果您看到 ExtractMonth 的文档,它只是 postgreSQL 的函数,如果您将 postgreSQL 函数与 MySQL 数据库一起使用,当然会出错。
我建议使用原始查询,或将数据库更改为 postgres。