Django's DecimalField represenation in admin as 0,00 values

1,093 views
Skip to first unread message

orschiro

unread,
May 2, 2012, 5:07:16 AM5/2/12
to Django users
I want to represent a price in my model in the form of 0,00. This is
the part of my model.

price = models.DecimalField(max_digits=5, decimal_places=2,
default=Decimal('0.00'))

However, prices such as 2.10 are still represented as 2.1.

The second issue would be to replace the . with a ,. However
default=Decimal('0,00') of course is not possible since , is not a
valid literal for Decimal.

Any ideas?

orschiro

unread,
May 3, 2012, 12:30:45 AM5/3/12
to Django users
Anybody?

Mike Dewhirst

unread,
May 3, 2012, 12:47:42 AM5/3/12
to django...@googlegroups.com
Never tried this but I certainly want to eventually. So in settings.py
at the least you would need ...

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

https://docs.djangoproject.com/en/1.4/topics/i18n/

If you figure out the neatest way for different users in different
places to switch locales I would like to hear how it is done.

Cheers

Mike





jrief

unread,
May 3, 2012, 2:21:46 AM5/3/12
to django...@googlegroups.com
Internally a price is always handled and stored as 0.00. Thats the way software and databases work.

What you can do is to change the representation of your Decimal in your frontend and backend. In Django, changing this in the frontend its easy, just add USE_L10N = True to your settings.py and add localized=True to all occurrences of form fields handling a Decimal (see https://docs.djangoproject.com/en/1.4/ref/forms/fields/#localize)

The problem is the backend, because you are not specifying these form field. What I do is to add a mixin class to my admin classes, as described here: https://github.com/divio/django-shop/pull/146#issuecomment-4720340 and here https://github.com/divio/django-shop/blob/master/shop/admin/mixins.py

For your "2.1" problem, set decimal_places to 2 DecimalField.

orschiro

unread,
May 4, 2012, 11:06:55 AM5/4/12
to Django users
Thanks for your replies.

Regarding the localize option, I tried to use it in my models.py [1].

However, I get the following error.

Field.__init__(self, verbose_name, name, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'localize'

[1] http://pastebin.com/9TkwWnNY

Can localize only be used for forms but not for my models that are
eventually represented in the admin backend form?

Robert

On May 3, 8:21 am, jrief <jacob.r...@gmail.com> wrote:
> Internally a price is always handled and stored as 0.00. Thats the way
> software and databases work.
>
> What you can do is to change the representation of your Decimal in your
> frontend and backend. In Django, changing this in the frontend its easy,
> just add USE_L10N = True to your settings.py and add localized=True to all
> occurrences of form fields handling a Decimal (seehttps://docs.djangoproject.com/en/1.4/ref/forms/fields/#localize)
>
> The problem is the backend, because you are not specifying these form
> field. What I do is to add a mixin class to my admin classes, as described
> here:https://github.com/divio/django-shop/pull/146#issuecomment-4720340
> and
> herehttps://github.com/divio/django-shop/blob/master/shop/admin/mixins.py
Reply all
Reply to author
Forward
0 new messages