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?

那有意义吗?

更多相关文章

  1. AttributeError:“MatrixFactorizationModel”对象没有属性“sav
  2. python 按位置关系输出矩阵元素
  3. Python------类的结构细分,异常处理,方法,属性
  4. Python进阶----类的结构(公有成员 , 私有成员(私有属性,私有方法
  5. 使用python如何在列表列表中找到元素,而关键元素是内部列表的元素
  6. AttributeError:'Flask'对象没有属性'login_manager' - Login_Ma
  7. 对numpy数组的每n个元素求平均值
  8. 008 Python基本语法元素小结
  9. 关于Python的属性、参数、方法的解释、区别

随机推荐

  1. C++编写数据库备份程序,支持MSSQL\Oracle
  2. 保持最新的一个字段值,直到它发生变化,然后
  3. mysql-proxy主从搭建读写分离全过程
  4. MyBatis排序时使用order by 动态参数时需
  5. [Step By Step]使用SAP Business Objects
  6. 如何在表单行SQL中检查是否有任何值为Nul
  7. 麻烦问下,为什么我的mysql存储过程一次调
  8. Ruby 1.9 + MySQL中发生访问错误
  9. MySql索引的优缺点
  10. [转]swoole安装报错详解 mysqlnd_find_ch