Django模型选择:使用元组的第一个元素

时间:2023-01-14 19:37:02

I have a model with choices attribute like this:

我有一个带有这样的选择属性的模型:

commonness_choices = (
    ("1", "very uncommon"),
    ("2", "uncommon"),
    ("3", "common"),
    ("4", "very common"),
)

class Event(models.Model):
    commonness = models.CharField(max_length=30, choices=commonness_choices)

When I try to use Event.commonness in views.py, it uses the second element of each choice tuple. For example, if I do this:

当我尝试在views.py中使用Event.commonness时,它使用每个选择元组的第二个元素。例如,如果我这样做:

event = Event.objects.get(pk=1)
event.commonness = "uncommon"

event.commonness is set to ("2", "uncommon"). When event.commonness is used, it uses the second element, "uncommon", instead of the first element, "2". Is there a way to select and use the first element of tuple? I wish to use both the first element and the second element of tuple in different cases.

event.commonness设置为(“2”,“uncommon”)。当使用event.commonness时,它使用第二个元素“uncommon”,而不是第一个元素“2”。有没有办法选择和使用元组的第一个元素?我希望在不同情况下同时使用元组的第一个元素和第二个元素。

2 个解决方案

#1


0  

I would setup your choices like this:

我会像这样设置你的选择:

class Event(models.Model):
    VERY_UNCOMMON = 0
    UNCOMMON = 1
    COMMON = 2
    VERY_COMMON = 3

    COMMONNESS_CHOICES = (
        (VERY_UNCOMMON, _('Very Uncommon')),
        (UNCOMMON, _('Uncommon')),
        (COMMON, _('Common')),
        (VERY_COMMON, _('Very Common')),
        (CANCELED, _('Canceled')),
    )
    commonness_choices = models.IntegerField(
        choices=COMMONNESS_CHOICES, default=VERY_UNCOMMON)

For example, whenever you want to call the number, you can do Event.VERY_UNCOMMON. That will return 0 (the first element).

例如,每当您想要拨打该号码时,您都可以进行Event.VERY_UNCOMMON。那将返回0(第一个元素)。

Does that make sense?

那有意义吗?

#2


0  

I changed the code so that event.commonness sets to "2" and used event.get_commonness_display() to view the second element of tuple. And it works as I want it to. Thank you for help in comments!

我更改了代码,以便event.commonness设置为“2”并使用event.get_commonness_display()来查看元组的第二个元素。它可以按照我的意愿运作。感谢您的评论帮助!

#1


0  

I would setup your choices like this:

我会像这样设置你的选择:

class Event(models.Model):
    VERY_UNCOMMON = 0
    UNCOMMON = 1
    COMMON = 2
    VERY_COMMON = 3

    COMMONNESS_CHOICES = (
        (VERY_UNCOMMON, _('Very Uncommon')),
        (UNCOMMON, _('Uncommon')),
        (COMMON, _('Common')),
        (VERY_COMMON, _('Very Common')),
        (CANCELED, _('Canceled')),
    )
    commonness_choices = models.IntegerField(
        choices=COMMONNESS_CHOICES, default=VERY_UNCOMMON)

For example, whenever you want to call the number, you can do Event.VERY_UNCOMMON. That will return 0 (the first element).

例如,每当您想要拨打该号码时,您都可以进行Event.VERY_UNCOMMON。那将返回0(第一个元素)。

Does that make sense?

那有意义吗?

#2


0  

I changed the code so that event.commonness sets to "2" and used event.get_commonness_display() to view the second element of tuple. And it works as I want it to. Thank you for help in comments!

我更改了代码,以便event.commonness设置为“2”并使用event.get_commonness_display()来查看元组的第二个元素。它可以按照我的意愿运作。感谢您的评论帮助!