【问题标题】:How to store a string that points to a boolean value in a django sqlite database如何在 django sqlite 数据库中存储指向布尔值的字符串
【发布时间】:2016-12-23 20:34:36
【问题描述】:

我想要发生的事情:

我正在 django 中构建一个 PTO(带薪休假)应用程序。我希望用户能够在 4-5 种不同的休假类型之间进行选择,例如“Jusry Duty”、“Voting”、“Emergency”等,并且我希望这些字符串指向 True 或 False 值,以确定是否休假类型是否计入用户的总 PTO 小时数。

问题:

正确的 True 或 False 值被保存到数据库中,但是当我从 django admin 中查看记录时,如果它是 True 值,它总是显示为“PTO”,如果它是 False 值,它总是显示被称为“陪审团义务”。我相信它表现得这样的原因是因为“PTO”是元组中的第一个 True 值,而“Jury Duty”是元组中的第一个 False 值。

我的尝试:

我现在拥有的是我的 PtoHistory 模型上的一个布尔字段,它接受一个 LEAVE_CHOICES 元组。

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

如果我选择“紧急”并保存它,这就是它在 django 管理员中的外观。

https://i.stack.imgur.com/UEgXM.png

【问题讨论】:

    标签: django sqlite django-models


    【解决方案1】:

    您可以添加另一个字段is_chargeable 并将leave_type 设为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_type 的值,如果它具有可收费的值(“pto”或“紧急”),则is_chargeable 字段应为True

    【讨论】:

    • 谢谢埃文斯穆里西。我正在考虑做这样的事情,但我不确定如何将两者链接在一起以保持数据库的完整性。我会让你知道结果如何。
    【解决方案2】:

    老实说,您对BooleanFieldchoices 存在一些重大误解。

    首先BooleanField只能有TrueFalse的值,不能取choices

    在 django 中,您应该使用 CharFieldchoices。它不会存储什么是真什么是假,而是记录您选择在该字段上存储的内容:

    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_CHOICES 中的元组是什么意思?第一个值是存储在数据库中的字符串,第二个值是 django 中的前端表示。

    通过上述修复,您可以在管理界面中有一个下拉菜单,然后选择其中一个选项作为leave_type

    请花一些时间阅读有关model fields,尤其是choices section 的django 文档。

    【讨论】:

    • 感谢尚旺的回复。 Boolean Fields 可以接受选择,选择只是 True 或 False 的表示。
    猜你喜欢
    • 2019-02-10
    • 2010-10-25
    • 2012-07-22
    • 2021-12-04
    • 2011-03-10
    • 1970-01-01
    • 2016-05-25
    • 2016-11-10
    • 1970-01-01
    相关资源
    最近更新 更多