총 3 개의 선택 값 0, 6, 19로 선택 필드 위젯을 렌더링하는 양식이 있습니다 (이들은 "Decimal")빈 문자열이나 십진수 ('0') 사이의 선택 필드가 렌더링되지 않음
값이 6 또는 19 인 모델 폼을 통해 객체를 편집 할 때 위젯은 렌더링시 적절한 객체를 선택하지만 객체가 "Decimal ('0') 값과 함께 저장되면 (새로운 오브젝트를 추가 할 때 목록이 모델에서 채워지지 않기 때문에, 수동 첨가) empty_label
Populator :
taxlevels = [('', u'---'),]
taxlevels += Metadata.objects.values_list('value', 'display'). \
filter(attribute__startswith='TAX').order_by('attribute')
이렇게하려면 렌더링 방법을 서브 클래스 화해야합니까?
고맙습니다.
편집 : 추가 된 코드 조각 (스팸 싶지 않았다) 형태가
:
class ProductForm(forms.ModelForm):
""" Product Model field specifications for new/edit.
"""
class Meta:
model = Product
def clean(self):
cleaned_data = self.cleaned_data
if cleaned_data.get('tax_level') == '--':
msg = _('Choose a VAT percentage.')
self._errors['tax_level'] = ErrorList([msg])
return cleaned_data
description = forms.CharField(
label = _('Notes'),
help_text = _('Max. 100 characters'),
max_length = 100,
widget = forms.Textarea(attrs =
{'rows': 2, 'cols': 40, 'maxlength': 100}
),
required = False,
)
# -- snip --
btwlevels = [('', u'---'),]
btwlevels += Metadata.objects.values_list('value', 'display'). \
filter(attribute__startswith='BTW').order_by('attribute')
tax_level = forms.ChoiceField(
label = _('VAT'),
choices = btwlevels,
required = True,
)
모델 :이 도움이
class Product(models.Model):
""" Product Model
"""
objects=UserFilteredManager()
owner = models.ForeignKey(User, editable=False)
order = models.ForeignKey(Order, editable=False)
name = models.CharField(_('Name'), max_length=50)
description = models.CharField(_('Notes'), max_length=100)
amount = models.DecimalField(_('Amount'), max_digits=10, decimal_places=2)
unit_price = models.DecimalField(_('Unit price'), max_digits=10,
decimal_places=2)
tax_level = models.DecimalField(_('Tax'), max_digits=3, decimal_places=1)
희망, 나는를 확인합니다 그 동안 TypedChoiceField.
고맙습니다.