我有一個窗體呈現一個選擇區域小部件總共有3個選擇值:0,6,19(這些類型是「Decimal」 )Choicefield渲染不區分空字符串或小數('0')
當通過具有值6或19的模型窗體編輯對象時,窗口小部件在渲染時選擇了正確的窗體,但是當對象存儲的值爲「十進制('0')時,它已選擇empty_label(這是手動添加,因爲添加新對象時該列表不是從模型填充)
填充器:
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。
再次感謝。