【发布时间】:2017-05-08 16:19:36
【问题描述】:
PtoHistory 模型:
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 管理员中的“leave_type”更改为“紧急”时,它都会在 django 管理员中显示为“PTO”,因为它们都是 True,但 PTO 首先出现在元组中,所以它在 django 管理员中显示“PTO”。所有为 False 的选项都会发生同样的事情。如果选择并保存“军事休假”,当在 django 管理员中查看时,它会显示为“陪审团职责”,因为“陪审团职责”是元组中的第一个错误。
我想要发生的事情:
如果用户选择“丧亲之痛”,我希望“丧亲之痛”作为所选选项显示在 django 管理员中,并且我希望它对应于 False 值,这意味着它不会计入员工的 PTO小时。
我希望我想要的输出得到理解,如果需要,我可以提供更多说明或编码 sn-ps。老实说,我什至不知道我是否以正确的方式去做。因此,如果我需要以任何方式重组我的模型,我将不胜感激。
睡在上面之后,我认为 python 字典将是解决这个问题的好方法,我可以保存键/值对,但我不知道 django 模型字段类型“字典”。
【问题讨论】:
标签: django django-models django-admin