Note that there are some explanatory texts on larger screens.

plurals
  1. POChoicefield render does not distinct between empty string or Decimal('0')
    text
    copied!<p>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').</p> <p>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)</p> <p>Populator:</p> <pre><code>taxlevels = [('', u'---'),] taxlevels += Metadata.objects.values_list('value', 'display'). \ filter(attribute__startswith='TAX').order_by('attribute') </code></pre> <p>Need I subclass the render method to do this?</p> <p>Thanx!</p> <p>EDIT: Added code snippets (didn't wanna spam ;)</p> <p>The form:</p> <pre><code>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, ) </code></pre> <p>The Model:</p> <pre><code>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) </code></pre> <p>hope this helps, I'll check the TypedChoiceField in the meantime.</p> <p>Thanx again.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload