Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django models Django和布尔字段_Django Models - Fatal编程技术网

Django models Django和布尔字段

Django models Django和布尔字段,django-models,Django Models,我有一个django模型,它水平记录多个用户偏好 class Preferences(models.Model): user = models.ForeignKey(CustomUser,on_delete='CASCADE') choice1 = models.BooleanField() choice2 = models.BooleanField() choice3 = models.BooleanField() choice4 = models.Bo

我有一个django模型,它水平记录多个用户偏好

class Preferences(models.Model):
    user = models.ForeignKey(CustomUser,on_delete='CASCADE')
    choice1 = models.BooleanField()
    choice2 = models.BooleanField()
    choice3 = models.BooleanField()
    choice4 = models.BooleanField()
    choice5 = models.BooleanField()
我正在尝试实现以下SQL查询:

(select 
(case when choice1 = True then 1 else 0 end) +
(case when choice2 = True then 1 else 0 end) +
(case when choice3 = True then 1 else 0 end) +
(case when choice4 = True then 1 else 0 end) +
(case when choice5 = True then 1 else 0 end) + as choice_sum
from Preferences)
在Django我该怎么做

如果您想知道,我将水平存储它们,因为每个用户都必须记录所有选项的首选项,这些选项将在将来增加,我不需要多行不必要的用户FK

编辑:

我意识到我的问题可能有点奇怪。我的目标是最终运行一个查询,选择任何选项字段至少有一个True的记录

我的目标是最终运行一个查询,在其中选择记录 至少有一个选项字段为True

使用Django过滤器+

from django.db.models import Q
.....
.....

choices = Preferences.objects.filter(Q(choice1=True) | Q(choice2=True) | Q(choice3=True) | Q(choice4=True) | Q(choice5=True))