Django模型字段在django admin中没有正确显示

时间:2016-12-22 23:52:09

标签: django django-models django-admin

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

问题:

每当我更改" leave_type"例如,django管理员内部的" Emergency"它将显示为" PTO"在django管理员中,因为它们都是True,但PTO首先出现在元组中,因此它显示了" PTO"在django管理员。对于所有False选项都会发生同样的事情。如果"军事假"被选中并保存,当在django管理员中查看时,它显示为" Jury Duty"因为" Jury Duty"是元组中的第一个错误。

dango admin pic

我想要发生什么:

如果用户选择"丧亲之痛",我想要"丧亲之痛"在django admin中显示为选择的选项,我希望它与False值相对应,这意味着它不会计入员工的PTO小时数。

我希望理解我想要的输出,如果需要,我可以提供更多说明或代码片段。老实说,我甚至不知道我是否以正确的方式解决这个问题。因此,如果我需要以任何方式重构我的模型,我会欣赏一些方向。

睡觉之后,我认为python词典是解决这个问题的好方法,我可以保存键/值对,但我不知道django模型字段类型"词典&#34 ;.

1 个答案:

答案 0 :(得分:0)

您可以将其设为CharField,这样您和用户就可以更轻松地选择 Live Type 的方式,就像这样。

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(max_length=225, choices=LEAVE_CHOICES)

    def __str__(self):
        return self.user.username

我会定义LEAVE_CHOICE外部模型,然后在forms.py中定义单选按钮小部件之后,您会发现类似的here

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?
    )

class PtoHistory(models.Model):
    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

forms.py

class PtoHistoryModelForm(forms.ModelForm):
    class Meta:
        model = MyModel
        fields = ['leave_start_data', 'leave_end_data', 'leave_type']
        widgets = {
            'leave_type': forms.RadioSelect
        }