Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>floatformat</code> expects its argument to be something it can convert to a <code>Decimal</code> instance, not a form field. <code>form.price_retail</code> is a form field instance (a <code>DecimalField</code>, to be specific).</p> <p><code>DecimalField</code> does accept a <a href="https://docs.djangoproject.com/en/dev/ref/forms/fields/#django.forms.DecimalField.decimal_places" rel="nofollow"><code>decimal_places</code></a> argument, but that's for input validation rather than rounding of existing values.</p> <p>I think you'll need to either <a href="http://docs.python.org/2/library/decimal.html#decimal.Decimal.quantize" rel="nofollow"><code>quantize</code></a> the value yourself - probably in the form's <code>__init__</code> method, or possibly in a subclassed field type or even a custom widget - or render the input element yourself so you can use <code>floatformat</code> directly on the HTML <code>input</code>'s <code>value</code> attribute. I think the simplest way is to do it in the <code>__init__</code>, something like this:</p> <pre><code>class ProductForm(forms.ModelForm): # Meta as above def __init__(self, *args, **kwargs): if 'instance' in kwargs: kwargs['instance'].price_retail = kwargs['instance'].price_retail.quantize(Decimal('0.01')) super(ProductForm, self).__init__(*args, **kwargs) self.fields['price_retail'].widget.attrs['class'] = 'input-price' self.fields['price_retail'].decimal_places = 2 </code></pre> <p>You can of course use whatever rounding strategy you like, as documented for quantize.</p> <p>I <em>think</em> this will not actually change the <code>price_retail</code> value if you submit the form without editing it, because the field will appear not to have changed from its initial value. But I haven't tested that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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