django型号选择列表 [英] django models choices list

查看:145
本文介绍了django型号选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.7.2,并且已经给出了一些代码以供选择列表放在模型中。



这里是代码: p>

  YOB_TYPES =选择(*(
((0,'select_yob',_('选择出生年份)) ,
(2000,'to_present',_('2000 to Present')))+
tuple((i,str(i))for x in xrange(1990,2000))+
(1,'unspecified',_('不想回答')))

....
year_of_birth_type = models.PositiveIntegerField(choices = YOB_TYPES,default = YOB_TYPES .select_yob,validators = [MinValueValidator(1)])
....

上面的代码给出了不正确的选择列表,如下所示。我已经读过几个SO帖子谷歌搜索和搜索文档,但我被困住了,我在圈子里。



这是当前代码显示选择列表,这是错误的: / p>



但是,我想要选择列表显示如下:



解决方案

你应该把最后一个元组元组:

  YOB_TYPES =选择(*(
((0,'select_yob',_出生')),
(2000,'to_present',_('2000 to Present')))+
tuple((i,str(i))for x in xrange(1990,2000) )+
((1,'unspecified',_('不想回答'))))


I am using django 1.7.2 and I have been given some code for a choices list to be placed in a model.

Here is the code:

YOB_TYPES = Choices(*(
    ((0, 'select_yob', _(' Select Year of Birth')),
     (2000, 'to_present', _('2000 to Present'))) +
    tuple((i, str(i)) for i in xrange(1990, 2000)) +
    (1, 'unspecified', _('Prefer not to answer')))
)
....
year_of_birth_type = models.PositiveIntegerField(choices=YOB_TYPES, default=YOB_TYPES.select_yob, validators=[MinValueValidator(1)])
....

The above code gives the incorrect select list as shown below. I have read several SO posts & google searches and scoured the docs, but I am stuck and I am going around in circles.

This is how the current code displays the select list, which is wrong:

However, I want the select list to be displayed as follows:

解决方案

You should wrap the last tuple in another tuple:

YOB_TYPES = Choices(*(
    ((0, 'select_yob', _(' Select Year of Birth')),
     (2000, 'to_present', _('2000 to Present'))) +
    tuple((i, str(i)) for i in xrange(1990, 2000)) +
    ((1, 'unspecified', _('Prefer not to answer')),))
)

这篇关于django型号选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆