如何在django sqlite数据库中存储指向布尔值的字符串
我希望发生什么: 我正在django中构建一个PTO(带薪休假)应用程序。我希望用户能够在4-5种不同的休假类型之间进行选择,例如“司法责任”、“投票”、“紧急情况”等,我希望这些字符串指向一个真值或假值,该值将确定休假类型是否可根据用户的总PTO时数计费 问题: 正确的真值或假值保存到数据库中,但当我从django admin中查看记录时,如果它是真值,则始终显示为“PTO”,如果它是假值,则始终显示为“Jury Duty”。我相信它这样做的原因是因为“PTO”是元组中的第一个真值,“Jury Duty”是元组中的第一个假值 我所尝试的: 我现在拥有的是PtoHistory模型中的一个布尔字段,它接受一组LEAVE_选项如何在django sqlite数据库中存储指向布尔值的字符串,django,sqlite,django-models,Django,Sqlite,Django Models,我希望发生什么: 我正在django中构建一个PTO(带薪休假)应用程序。我希望用户能够在4-5种不同的休假类型之间进行选择,例如“司法责任”、“投票”、“紧急情况”等,我希望这些字符串指向一个真值或假值,该值将确定休假类型是否可根据用户的总PTO时数计费 问题: 正确的真值或假值保存到数据库中,但当我从django admin中查看记录时,如果它是真值,则始终显示为“PTO”,如果它是假值,则始终显示为“Jury Duty”。我相信它这样做的原因是因为“PTO”是元组中的第一个真值,“Jury
class PtoHistory(models.Model):
LEAVE_CHOICES = (
(True, 'PTO'), #is chargeable?
(False, 'Jury Duty'), #is chargeable?
(False, 'Voting'), #is chargeable?
(False, 'Military Leave'), #is chargeable?
(False, 'Bereavement'), #is chargeable?
(True, 'Emergency'), #is chargeable?
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
leave_start_date = models.DateTimeField(auto_now=False,auto_now_add=False)
leave_end_date = models.DateTimeField(auto_now=False, auto_now_add=False)
leave_type = models.BooleanField(choices=LEAVE_CHOICES)
def __str__(self):
return self.user.username
如果我选择“Emergency”并保存它,这就是django管理员中的外观
老实说,您对
布尔域和选项有一些重大误解
首先,BooleanField
只能有True
或False
值,不能采用选项
在django中,您应该使用CharField
以及选项
。它不会存储什么是正确的,什么是错误的,但会记录您选择在该字段中存储的内容:
class PtoHistory(models.Model):
LEAVE_CHOICES = (
('pto', 'PTO'), #is chargeable?
('jury_duty', 'Jury Duty'), #is chargeable?
('voting', 'Voting'), #is chargeable?
('military', 'Military Leave'), #is chargeable?
('bereavement', 'Bereavement'), #is chargeable?
('emergency', 'Emergency'), #is chargeable?
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
leave_start_date = models.DateTimeField(auto_now=False,auto_now_add=False)
leave_end_date = models.DateTimeField(auto_now=False, auto_now_add=False)
leave_type = models.CharField(max_length=20, choices=LEAVE_CHOICES)
def __str__(self):
return self.user.username
那么LEAVE\u CHOICES
中的元组是什么意思呢?第一个值是存储在数据库中的字符串,第二个值是django中的前端表示
使用上述修复程序,您可以在管理界面中有一个下拉列表,然后选择其中一个选项作为离开类型
请花一些时间阅读django doc,尤其是。您可以添加另一个字段是可收费的,并使离开类型为CharField
。像这样:
class PtoHistory(models.Model):
LEAVE_CHOICES = (
('pto', 'PTO'), #is chargeable?
('jury_duty', 'Jury Duty'), #is chargeable?
('voting', 'Voting'), #is chargeable?
('military_leave', 'Military Leave'), #is chargeable?
('bereavement', 'Bereavement'), #is chargeable?
('emergency', 'Emergency'), #is chargeable?
)
user = models.ForeignKey(User, on_delete=models.CASCADE)
leave_start_date = models.DateTimeField(auto_now=False,auto_now_add=False)
leave_end_date = models.DateTimeField(auto_now=False, auto_now_add=False)
leave_type = models.CharField(choices=LEAVE_CHOICES, max_length=100)
is_chargeable = models.BooleanField(default=False)
def __str__(self):
return self.user.username
def save(self, *args, **kwargs):
if self.leave_type == 'pto' or self.leave_type == 'emergency':
self.is_chargeable = True
else:
self.is_chargeable = False
super().save(*args, **kwargs)
在save()
方法中,您可以检查leave\u type
的值,如果该值('pto'或'emergency')是可计费的,则是可计费的
字段应为真
谢谢Evans Murithi。我曾想过做类似的事情,但我不确定如何将两者链接在一起以保持数据库的完整性。我会让你知道结果的。谢谢你的回复。布尔字段可以接受选择,这些选择只是真或假的表示。