Django 4 如何对布尔字段查询集进行注释?
Django 4 how to do annotation over boolean field queryset?
我发现 this 关于查询集注释的讨论。我想知道,是否实施过?
当我运行下面的注解:
x = Isolate.objects.all().annotate(sum_shipped=Sum('shipped'))
x[0].sum_shipped
>>> True
shipped
是一个布尔字段,所以我希望这里的发货实例数设置为 True。相反,我得到 True 或 1。这是非常不方便的行为。
还有 关于 Whosebug 的讨论。然而,这只涵盖了 django 1.
我期待 Sum([True, True, False]) -> 2
这样的结果。
相反,返回 True
。
似乎从那以后就没有动过了。第二个 link 中的讨论仍然是最先进的吗???
现在 Django 已经是第 4 版了,还有比这更好的统计数据库内容的方法吗?
我目前的数据库是用于开发和测试的sqlite3,但在生产中将是Postgres。我非常希望获得独立于数据库的结果。
SUM(bool_field) on Oracle will return 1 also, because bools in Oracle are just a bit (1 or 0). Postgres has a specific bool type, and you can't sum a True/False without an implicit conversion to int. This is why I say SUM(bool) is only accidentally supported for a subset of databases. The field type that is returned is based on the backend get_db_converters and the actual values that come back.
示例模型
class Isolate(models.Model):
isolate_nbr = models.CharField(max_length=10, primary_key=True)
growing = models.BooleanField(default=True)
shipped = models.BooleanField(default=True)
....
我想你在这里有几个选择,目前注释不是其中之一。
from django.db.models import Sum, Count
# Use aggregate with a filter
print(Isolate.objects.filter(shipped=True).aggregate(Sum('shipped')))
# Just filter then get the count of the queryset
print(Isolate.objects.filter(shipped=True).count())
# Just use aggregate without filter (this will only aggregate/Sum the True values)
print(Isolate.objects.aggregate(Sum('shipped')))
# WON'T WORK: annotate will only annotate on that row whatever that row's value is, not the aggregate across the table
print(Isolate.objects.annotate(sum_shipped==Count('shipped')).first().sum_shipped)
我发现 this 关于查询集注释的讨论。我想知道,是否实施过?
当我运行下面的注解:
x = Isolate.objects.all().annotate(sum_shipped=Sum('shipped'))
x[0].sum_shipped
>>> True
shipped
是一个布尔字段,所以我希望这里的发货实例数设置为 True。相反,我得到 True 或 1。这是非常不方便的行为。
还有
我期待 Sum([True, True, False]) -> 2
这样的结果。
相反,返回 True
。
似乎从那以后就没有动过了。第二个 link 中的讨论仍然是最先进的吗???
现在 Django 已经是第 4 版了,还有比这更好的统计数据库内容的方法吗?
我目前的数据库是用于开发和测试的sqlite3,但在生产中将是Postgres。我非常希望获得独立于数据库的结果。
SUM(bool_field) on Oracle will return 1 also, because bools in Oracle are just a bit (1 or 0). Postgres has a specific bool type, and you can't sum a True/False without an implicit conversion to int. This is why I say SUM(bool) is only accidentally supported for a subset of databases. The field type that is returned is based on the backend get_db_converters and the actual values that come back.
示例模型
class Isolate(models.Model):
isolate_nbr = models.CharField(max_length=10, primary_key=True)
growing = models.BooleanField(default=True)
shipped = models.BooleanField(default=True)
....
我想你在这里有几个选择,目前注释不是其中之一。
from django.db.models import Sum, Count
# Use aggregate with a filter
print(Isolate.objects.filter(shipped=True).aggregate(Sum('shipped')))
# Just filter then get the count of the queryset
print(Isolate.objects.filter(shipped=True).count())
# Just use aggregate without filter (this will only aggregate/Sum the True values)
print(Isolate.objects.aggregate(Sum('shipped')))
# WON'T WORK: annotate will only annotate on that row whatever that row's value is, not the aggregate across the table
print(Isolate.objects.annotate(sum_shipped==Count('shipped')).first().sum_shipped)