views:

181

answers:

2

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.

A: 

If I understand your question correctly, I would first try it out with TypedChoiceField with a custom empty_value that would be handled manually in code.

But if your showed more code, it would be helpful in answering in greater detail

gbsmith
A: 

Solved it!!

The problem was that the value taken from the database was Decimal('0') and the one contained in option_value was Decimal('0.0'). For calculations this runs without error. But ..

Django (ver. 1.1.1) does a check in function render_option (file: forms/widgets.py, line: 411), when to render 'value is selected'

Since "0" is not "0.0" this fails and thus in a bound modelform the proper value in the select widget will never be selected.

3 hours of my life out the window :)

GerardJP