긴 코드는 사용 가능하지만 질문 자체는 짧습니다. 5.1.37 mysqldb 1.2.2 파이썬 2.5Django가 생성되지 않음 booleanfield를 나타내는 html select리스트에 대해 "selected = 'selected'"
에 모델
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"),)
를 들어 사용자 정의 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
로 : MySQL 서버 버전 장고 1.2.1을 사용
템플릿 코드
{% 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 %}
,363,210
결과는 다음 이렇게 qualification_type의 HTML 선택리스트는 적절한 <option value="1" selected="selected">WGH</option>
하지만 is_active 대한 적절한 옵션을 선택 받고 있지 않은 (is_active 대해 생성 된 HTML에는 selected="selected"
없다)지고
<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>
생성된다.
이전에 사용 했었습니다. 부울 선택을 0과 1에 매핑 했으므로 mysql과 python과 잘 맞습니다. 나는 어떻게 든 정확한 HTML을 생성하는 것을 멈추는 시점을 알기를 놓쳤습니다. 하지만 장고 - 1.2.1 이전 버전에서는 효과가 있었기 때문에 긍정적입니다.
변경 STATUS_CHOICES = ((0) '비활성'(1) '활성') STATUS_CHOICES에 = ((거짓) '비활성'(사실, '활성')) 및 강요 = int to coerce = bool은이 문제를 해결할 것입니다. 그러나 이전에 완벽 했었던 이유가 지금 중단 된 이유를 알고 싶습니다. 나는 Django 문서 나 릴리스 노트에서 아무 것도 찾을 수 없었다. 그래서 나는 그것을 묻는다. – chefsmart
기록을 위해 나는 원래 Django1.0 이전의 상태 인 STATUS_CHOICES = ((False, 'Inactive'), (True, 'Active'))로 시작했습니다. 작동하지 않아 STATUS_CHOICES = ((0, 'Inactive'), (1, 'Active'))를 사용했습니다. 그리고 지금 모든것이 주문되었습니다 (내 대답은 아래 링크 참조). – chefsmart