【发布时间】:2010-08-10 17:12:49
【问题描述】:
请原谅长代码,但问题本身很短。
使用 Django 1.2.1 和 MySQL 服务器版本:5.1.37 和 Python 2.5 上的 mysqldb 1.2.2
对于模型
QUALIFICATION_TYPE_CHOICES = ((1, 'WGH'), (2, 'PQR'))
class Qualification(models.Model):
name = models.CharField(max_length=50)
qualification_type = models.PositiveSmallIntegerField(choices=QUALIFICATION_TYPE_CHOICES)
is_active = models.BooleanField("Item status",db_index=True,help_text="Only active items are visible on rest of this website")
def __unicode__(self):
return u'%s' % (self.name)
class Meta:
ordering = ['qualification_type', 'name']
unique_together = (("qualification_type", "name"),)
使用自定义模型表单
STATUS_CHOICES = ((0, 'Inactive'), (1, 'Active'))
class EditQualificationForm(forms.ModelForm):
name = forms.CharField(label='* Unique Name', max_length=50,help_text="(Required, max 50 characters)",widget=forms.TextInput(attrs={'class':'textInput',}))
qualification_type = forms.TypedChoiceField(label='Type',coerce=int,empty_value=None,choices=QUALIFICATION_TYPE_CHOICES,widget=forms.Select(attrs={'class':'selectInput',}))
is_active = forms.TypedChoiceField(label='* Is this Qualification active?',help_text="xyz",coerce=int,empty_value=0, choices=STATUS_CHOICES,widget=forms.Select(attrs={'class':'selectInput',}))
class Meta:
model = Qualification
模板代码
{% if form.non_field_errors %}
<div class="error">
{% for error in form.non_field_errors %}
<p class="errorField"><strong>{{ error }}</strong></p>
{% endfor %}
</div>
{% endif %}
{% for field in form.visible_fields %}
{% if field.errors %}
<div class="ctrlHolder error">
{% for error in field.errors %}
<p class="errorField"><strong>{{ error }}</strong></p>
{% endfor %}
{% else %}
<div class="ctrlHolder">
{% endif %}
{{ field.label_tag }}
{{ field }}
<p class="formHint">{{ field.help_text }}</p>
</div>
{% endfor %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
正在生成以下输出
<div class="ctrlHolder">
<label for="id_qualification_type">Type</label>
<select id="id_qualification_type" class="selectInput" name="qualification_type">
<option value="1" selected="selected">WGH</option>
<option value="2">PQR</option>
</select>
<p class="formHint"></p>
</div>
<div class="ctrlHolder">
<label for="id_is_active">* Is this Qualification active?</label>
<select id="id_is_active" class="selectInput" name="is_active">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
<p class="formHint">xyz</p>
</div>
因此,qualification_type 的 html 选择列表获得了正确的 <option value="1" selected="selected">WGH</option>,但未选择 is_active 的正确选项(为 is_active 生成的 html 中没有 selected="selected")。
这曾经在早期工作过。我已将布尔选项映射到 0 和 1,它非常适合 mysql 和 python。我不知何故错过了在什么时候停止生成正确的 html。但我很肯定它在 Django-1.2.1 之前有效。
【问题讨论】:
-
将 STATUS_CHOICES = ((0, 'Inactive'), (1, 'Active')) 更改为 STATUS_CHOICES = ((False, 'Inactive'), (True, 'Active')) 并强制=int to coerce=bool 可能会解决这个问题,但我想知道为什么它现在停止工作,而它之前是完美的。我在 Django 文档或发行说明中找不到任何内容,这就是我要问的原因。
-
为了记录,我最初是从 STATUS_CHOICES = ((False, 'Inactive'), (True, 'Active')) 开始的——那是在 Django1.0 之前的日子。它没有用,所以我使用了 STATUS_CHOICES = ((0, 'Inactive'), (1, 'Active'))。现在看来一切都已安排妥当(请参阅下面我的答案中的链接)。