使用元组时,ChoiceField不会显示空标签 - 我做错了什么? [英] ChoiceField doesn't display an empty label when using a tuple - am I doing something wrong?

查看:98
本文介绍了使用元组时,ChoiceField不会显示空标签 - 我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是



我将在数据库中保留有关比赛的数据。我想能够通过某些标准来搜索比赛 - 特别是比赛类型。



关于比赛类型



比赛类型保存在元组中。稍微缩短的例子:

  COMPETITION_TYPE_CHOICES =(
(1,奥运),
2,ISU锦标赛),
(3,大奖赛系列),

这些在模型中使用(再次 - 这是模型的缩写/简化版本):

  class Competition(models.Model):
name = models.CharField(max_length = 256)
type = models.IntegerField(choices = COMPETITION_TYPE_CHOICES)



搜索表单



我不想在搜索表单中要求字段,所以表单定义如下:

  class CompetitionSearchForm(forms.Form):
name = forms.CharField (required = False)
type = forms.ChoiceField(choices = COMPETITION_TYPE_CHOICES,required = False)



< h2>问题

我想要ChoiceField中的选择小部件disp放一个空的标签,但我不会得到一个。对此的任何帮助将非常感谢:)

解决方案

我找到了一种解决方案,违反DRY原则。不太干净,但是我想这样做。



根据文档的选择不一定是元组:


最后,请注意,选择可以是任何
可迭代对象 - 不一定是
列表或元组。这样可以动态构建
选项。但是如果你发现
你自己的黑客选择是
的动态,你可能会更好的
使用正确的数据库表与
ForeignKey。选择是为
静态数据变化不大,
如果有的话。


所以解决方案我现在要去的是:

  COMPETITION_TYPE_CHOICES = [
(1,'Olympic Games'),
(2,'ISU Championships'),
(3,'Grand Prix Series'),
]

COMP_TYPE_CHOICES_AND_EMPTY = [('','All' )] + COMPETITION_TYPE_CHOICES

然后:



<$
name = forms.CharField(required = False)
type = forms.ChoiceField(choices = COMP_TYPE_CHOICES_AND_EMPTY,required = False)p $ p> class CompetitionSearchForm(forms.Form)

模型保持不变。


What I'm trying to do

I'm going to be keeping data about competitions in my database. I want to be able to search the competitions by certain criteria - competition type in particular.

About competition types

Competition types are kept in a tuple. A slightly shortened example:

COMPETITION_TYPE_CHOICES = (
    (1, 'Olympic Games'),
    (2, 'ISU Championships'),
    (3, 'Grand Prix Series'),
)

These are used in the model like so (again - this is a shortened/simplified version of the model):

class Competition(models.Model):
    name = models.CharField(max_length=256)
    type = models.IntegerField(choices=COMPETITION_TYPE_CHOICES) 

The search form

I don't want the fields to be required in the search form, so the form is defined like this:

class CompetitionSearchForm(forms.Form):
    name = forms.CharField(required=False)
    type = forms.ChoiceField(choices=COMPETITION_TYPE_CHOICES,required=False)

The problem

I'd like the select widget in ChoiceField to display an empty label, but I don't get one. Any help with this would be much appreciated :)

解决方案

I've found a solution that works the way I want it to without violating the DRY principle. Not very clean, but it'll have to do I suppose.

According to the documentation choices don't have to be a tuple:

Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking choices to be dynamic, you're probably better off using a proper database table with a ForeignKey. choices is meant for static data that doesn't change much, if ever.

So the solution I'm going with for the moment is:

COMPETITION_TYPE_CHOICES = [
     (1, 'Olympic Games'),
     (2, 'ISU Championships'),
     (3, 'Grand Prix Series'),
]

COMP_TYPE_CHOICES_AND_EMPTY = [('','All')] + COMPETITION_TYPE_CHOICES

And then:

class CompetitionSearchForm(forms.Form):
    name = forms.CharField(required=False)
    type = forms.ChoiceField(choices=COMP_TYPE_CHOICES_AND_EMPTY, required=False)

The model stays the same as it was.

这篇关于使用元组时,ChoiceField不会显示空标签 - 我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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