开发者

Django not generating "selected='selected'" for html select list representing a booleanfield

开发者 https://www.devze.com 2023-01-11 13:05 出处:网络
Please excuse th开发者_JS百科e long code, but the question itself is short. Using Django 1.2.1 with MySQL Server version: 5.1.37 with mysqldb 1.2.2 on Python 2.5

Please excuse th开发者_JS百科e long code, but the question itself is short.

Using Django 1.2.1 with MySQL Server version: 5.1.37 with mysqldb 1.2.2 on Python 2.5

For the model

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"),)

with a custom modelform

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

template code

{% 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 %}

is generating the following output

<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>

so the html select list for qualification_type is getting a proper <option value="1" selected="selected">WGH</option> but the proper option for is_active is not getting selected (there is no selected="selected" in the generated html for is_active).

This used to work earlier. I had mapped boolean choices to 0 and 1 and it fit in nicely with mysql and python. I somehow missed to notice at what point of time this stopped generating the correct html. But I'm positive it worked pre-Django-1.2.1.


I am guessing it has got to do with the unnecessary empty_value on the is_active form field.


OK, I found it. I have gone through the release notes a couple of times, so I really don't know how I missed it.

http://docs.djangoproject.com/en/dev/releases/1.2/#booleanfield-on-mysql

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号