请原谅长代码,但问题本身很短。
在MySQL 2.5上使用带有MySQL服务器版本的Django 1.2.1:5.1.37和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>
所以qualified_type的html选择列表正在获得正确的<option value="1" selected="selected">WGH</option>
,但is_active的正确选项未被选中(生成的html中没有selected="selected"
for_active)。
这曾经比较早。我已经将布尔选择映射到0和1,它与mysql和python非常吻合。我在某种程度上错过了注意到什么时候停止生成正确的html。但我很肯定它在Django-1.2.1之前工作。
答案 0 :(得分:0)
我猜这与empty_value
表单字段中不必要的is_active
有关。
答案 1 :(得分:0)
好的,我找到了。我已经阅读了几次发行说明,所以我真的不知道我是如何错过它的。
http://docs.djangoproject.com/en/dev/releases/1.2/#booleanfield-on-mysql