gpt4 book ai didi

mysql - 在 Django : select_format 中使用条件注释时出错

转载 作者:行者123 更新时间:2023-12-01 07:48:53 25 4
gpt4 key购买 nike

尝试注释查询集时,我从 django 方法中收到 TypeError。
我在 Ubuntu 上使用 Django 1.11.12 和 MySQL 5.7.22。

这是我的models.py:

class Group(models.Model):
name = models.CharField(max_length=100)

class ActivationRecord(models.Model):
group = models.ForeignKey('directory.Group')
year = models.PositiveIntegerField()

如果一个组有给定年份的记录,则该组是“活跃的”。我正在尝试使用以下查询注释组是否处于事件状态:
Group.objects.annotate(active=Case(
When(activationrecord__year=2018, then=True), default=False, output_field=BooleanField))

当我运行时,我收到以下错误:
  File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/query.py", line 226, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 877, in execute_sql
sql, params = self.as_sql()
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 429, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 46, in pre_sql_setup
self.setup_query()
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 37, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 227, in get_select
sql, params = self.compile(col, select_format=True)
File "/home/vagrant/Envs/think/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 375, in compile
return node.output_field.select_format(self, sql, params)
TypeError: select_format() missing 1 required positional argument: 'params'

最佳答案

BooleanField需要作为函数调用,使用 ()

Group.objects.annotate(active=Case(
When(activationrecord__year=2018, then=True),
default=False, output_field=BooleanField()))

关于mysql - 在 Django : select_format 中使用条件注释时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com