django-shop icon indicating copy to clipboard operation
django-shop copied to clipboard

MoneyFieldWidget

Open jankopacek opened this issue 7 years ago • 3 comments

In demo (didn't try anywhere else) the MoneyField is rendered empty on a change form, due to value rendered with currency in number field.

class MoneyFieldWidget(forms.widgets.NumberInput):
    def render(self, name, value, attrs=None):
        input_field = super(MoneyFieldWidget, self).render(name, value, attrs)
        return format_html('{} <strong>{}</strong>', input_field, self.currency_code)

input field is returning: <input id="id_unit_price" name="unit_price" step="0.001" style="width: 75px; text-align: right" type="number" value="385.00 Kč" />

jankopacek avatar Sep 02 '16 13:09 jankopacek

Hmm. The currency should be appended after the input field. Strange that it's added inside the field. Does this also happen if you use Euro? This is how I deliver DjangoSHOP to my customers.

jrief avatar Sep 02 '16 21:09 jrief

Yes, EUR does the same. I will try to figure it out. For now I can provide only basic information:

Python 3.5.1 and this release of shop:

git+https://github.com/awesto/django-shop@4952f8f8884f618f56e82e1d0d1a23cc1a377613#egg=django_shop

jankopacek avatar Sep 05 '16 11:09 jankopacek

Problem is, that value returns MoneyInCZK('380.000'), which in case '%s % value prints '380.00 Kč' (that is also the behavior of django Input) Possible solution could be:

def render(self, name, value, attrs=None):        
        input_field = super(MoneyFieldWidget, self).render(name, value.as_decimal(), attrs)
        return format_html('{} <strong>{}</strong>', input_field, self.currency_code)

jankopacek avatar Sep 06 '16 08:09 jankopacek