Hi all,
I've got a form that renders a choicefield widget with a total of 3 choice values: 0, 6, 19 (these are of type "Decimal').
When editing an object via a modelform that has the value 6 or 19 the widget has selected the proper one when rendered, but when the object is stored with the value "Decimal('0') it has selected the empty_label (which was added manually, since the list is not populated from a model when adding new objects)
Populator:
taxlevels = [('', u'---'),]
taxlevels += Metadata.objects.values_list('value', 'display'). \
filter(attribute__startswith='TAX').order_by('attribute')
Need I subclass the render method to do this?
Thanx!
EDIT: Added code snippets (didn't wanna spam ;)
The form:
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,
)
The Model:
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)
hope this helps, I'll check the TypedChoiceField in the meantime.
Thanx again.