money field question

113 views
Skip to first unread message

Mike Dewhirst

unread,
May 21, 2018, 9:25:04 PM5/21/18
to Django users
I have just read ...

* Martin Fowler's Money base pattern (P488 to 495)
https://martinfowler.com/eaaCatalog/money.html
* https://github.com/poswald/python-money
* https://github.com/django-money/django-money
* https://github.com/limist/py-moneyed

... and I am spoiled for choice. I was going to just use a DecimalField
and CharField for currency and do my own thing when I decided to pull
Martin Fowler off my shelf and saw how careful I was going to need to be.

My question: Is Django planning to adopt one or other of the above
implementations?

I'm just trying to short-circuit more detailed analysis of these
packages and perhaps others I haven't yet found. If one is on the
horizon for Django I'll just go with that.

Thanks

Mike



Simon McConnell

unread,
May 22, 2018, 10:31:24 PM5/22/18
to Django users
I'm in a similar boat at the moment.  There is https://github.com/vimeo/py-money too.  Note that django-money uses py-moneyed which does not yet use Babel, so rendering of the correct symbol is limited to a few hardcoded currencies.


Mike Dewhirst

unread,
May 23, 2018, 10:05:06 PM5/23/18
to django...@googlegroups.com
On 23/05/2018 12:31 PM, Simon McConnell wrote:
> I'm in a similar boat at the moment.  There
> is https://github.com/vimeo/py-money too.

I looked at that but it probably won't ever support exchange rates. Not
sure yet if that is a show stopper because maybe forex is a separate
thing anyway. I do need exchange rates in another project and I'd like
choose a MoneyField which works in all projects.

> Note that django-money uses py-moneyed which does not yet use Babel,
> so rendering of the correct symbol is limited to a few hardcoded
> currencies.

Interesting. I don't use Babel yet. But it is on the horizon.

I'm beginning to see why Django doesn't (yet?) have a built-in
MoneyField and I'm still interested in experience with differences
between them.

Mike

>
>
> https://github.com/python-babel/babel
>
>
> On Tuesday, 22 May 2018 11:25:04 UTC+10, Mike Dewhirst wrote:
>
> I have just read ...
>
> * Martin Fowler's Money base pattern (P488 to 495)
> https://martinfowler.com/eaaCatalog/money.html
> <https://martinfowler.com/eaaCatalog/money.html>
> * https://github.com/poswald/python-money
> <https://github.com/poswald/python-money>
> * https://github.com/django-money/django-money
> <https://github.com/django-money/django-money>
> * https://github.com/limist/py-moneyed
> <https://github.com/limist/py-moneyed>
>
> ... and I am spoiled for choice. I was going to just use a
> DecimalField
> and CharField for currency and do my own thing when I decided to pull
> Martin Fowler off my shelf and saw how careful I was going to need
> to be.
>
> My question: Is Django planning to adopt one or other of the above
> implementations?
>
> I'm just trying to short-circuit more detailed analysis of these
> packages and perhaps others I haven't yet found. If one is on the
> horizon for Django I'll just go with that.
>
> Thanks
>
> Mike
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/762af31e-56fd-4976-bef2-f627c5673f38%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/762af31e-56fd-4976-bef2-f627c5673f38%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
May 28, 2018, 5:50:43 AM5/28/18
to django...@googlegroups.com
On 24/05/2018 12:03 PM, Mike Dewhirst wrote:
> On 23/05/2018 12:31 PM, Simon McConnell wrote:
>> I'm in a similar boat at the moment. There
>> is https://github.com/vimeo/py-money too.
>
> I looked at that but it probably won't ever support exchange rates.
> Not sure yet if that is a show stopper because maybe forex is a
> separate thing anyway. I do need exchange rates in another project and
> I'd like choose a MoneyField which works in all projects.

On reflection I don't think exchange rates are any sort of show-stopper.
I either agree on an exchange rate with the client ahead of time and do
a forex deal without a rate (and wear the loss or gain when actually
exchanged) or bill in AUD and expect AUD to be remitted. I don't think I
want the complication of the MoneyField doing what someone else thinks
should happen.


>
>> Note that django-money uses py-moneyed which does not yet use Babel,
>> so rendering of the correct symbol is limited to a few hardcoded
>> currencies.

But py-moneyed does claim a complete dictionary of ISO 4217 currencies.

It also supports Python 2.7 which is useful for me because I haven't yet
rebuilt apache/mod_wsgi for py3 on my Ubuntu VMs. I wish there was a
pill to cure procrastination.

poswald/python-money does the same and looks interesting. Might go there
and see how it works. It also has form and field helpers.

Mike
--

Climate Pty Ltd
PO Box 308
Mount Eliza
Vic 3930
Australia +61

T: 03 9034 3977
M: 0411 704 143


Simon McConnell

unread,
May 28, 2018, 4:07:18 PM5/28/18
to django...@googlegroups.com


On Mon., 28 May 2018, 7:50 pm Mike Dewhirst, <mi...@dewhirst.com.au> wrote:
On 24/05/2018 12:03 PM, Mike Dewhirst wrote:
> On 23/05/2018 12:31 PM, Simon McConnell wrote:
>> I'm in a similar boat at the moment. There
>> is https://github.com/vimeo/py-money too.
>
> I looked at that but it probably won't ever support exchange rates.
> Not sure yet if that is a show stopper because maybe forex is a
> separate thing anyway. I do need exchange rates in another project and
> I'd like choose a MoneyField which works in all projects.

On reflection I don't think exchange rates are any sort of show-stopper.
I either agree on an exchange rate with the client ahead of time and do
a forex deal without a rate (and wear the loss or gain when actually
exchanged) or bill in AUD and expect AUD to be remitted. I don't think I
want the complication of the MoneyField doing what someone else thinks
should happen.

Agreed. Currency exchange happens at the financial institution, not in our web apps. It would only be useful for estimation in my mind (I see no need for it in my application so haven't thought much about it). 



>
>> Note that django-money uses py-moneyed which does not yet use Babel,
>> so rendering of the correct symbol is limited to a few hardcoded
>> currencies.

But py-moneyed does claim a complete dictionary of ISO 4217 currencies.


https://github.com/limist/py-moneyed/blob/master/moneyed/localization.py only has the localisation configured for a handful of currencies, and not our precious AUD :). The shortest party would be adding them all in or doing https://github.com/limist/py-moneyed/issues/22

It also supports Python 2.7 which is useful for me because I haven't yet
rebuilt apache/mod_wsgi for py3 on my Ubuntu VMs. I wish there was a
pill to cure procrastination.

Work with what you've got (that being procrastination): http://www.structuredprocrastination.com

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/O51wKkudE_U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

Mike Dewhirst

unread,
May 28, 2018, 7:33:42 PM5/28/18
to django...@googlegroups.com
On 29/05/2018 6:06 AM, Simon McConnell wrote:
>
> https://github.com/limist/py-moneyed/blob/master/moneyed/localization.py
> only has the localisation configured for a handful of currencies, and
> not our precious AUD :). The shortest party would be adding them all
> in or doing https://github.com/limist/py-moneyed/issues/22
>

Look at the bottom of this file ...

https://github.com/poswald/python-money/blob/master/money/money.py


Mike Dewhirst

unread,
May 28, 2018, 7:51:50 PM5/28/18
to django...@googlegroups.com
On 29/05/2018 6:06 AM, Simon McConnell wrote:
>
>
> On Mon., 28 May 2018, 7:50 pm Mike Dewhirst, <mi...@dewhirst.com.au
googlecode/python-money
    |____________________________
            |       |            |
       py-moneyed   |   grengojbo/python-money
            |       |            |
            \_______/            |
                |                |
          django-money    poswald/python-money

 and

                vimeo/py-money

 py-moneyed is de-djangoed googlecode and django-money is googlecode
plus py-moneyed

I'm still procrastinating.
> <mailto:django-users%2Bunsu...@googlegroups.com>
> >> <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> >> To post to this group, send email to
> django...@googlegroups.com <mailto:django...@googlegroups.com>
> >> <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com>>.
> >> Visit this group at https://groups.google.com/group/django-users.
> >> To view this discussion on the web visit
> >>
> https://groups.google.com/d/msgid/django-users/762af31e-56fd-4976-bef2-f627c5673f38%40googlegroups.com
>
> >>
> <https://groups.google.com/d/msgid/django-users/762af31e-56fd-4976-bef2-f627c5673f38%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> >>
> >> For more options, visit https://groups.google.com/d/optout.
> >
>
>
> --
>
> Climate Pty Ltd
> PO Box 308
> Mount Eliza
> Vic 3930
> Australia +61
>
> T: 03 9034 3977
> M: 0411 704 143
>
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/O51wKkudE_U/unsubscribe.
> To unsubscribe from this group and all its topics, send an email
> to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAB-XqiXr4-jdqkjm8p3GJZo4gopoNK5BL6%2B-a%2BYezagf%3D0MWTw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAB-XqiXr4-jdqkjm8p3GJZo4gopoNK5BL6%2B-a%2BYezagf%3D0MWTw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Mike Dewhirst

unread,
May 29, 2018, 5:04:58 AM5/29/18
to django...@googlegroups.com
On 29/05/2018 6:06 AM, Simon McConnell wrote:
> Work with what you've got (that being procrastination):
> http://www.structuredprocrastination.com
>

You should see the pile of stuff I'm avoiding. It's worth a PhD in
procrastination

>> poswald/python-money does the same and looks interesting. Might go there
>> and see how it works. It also has form and field helpers.

Although it has all the currencies and includes the correct number of
decimals in its CURRENCY dict it ignores them in calculations. You can
calculate with lengthy decimal tails and round it all at the end.

Which might be quite reasonable. The alternative is to round everything
to the correct number of decimals for the nominated currency before a
calculation. Might speak to an accountant or banker on that. I suspect
bankers round it at every step and catch the shavings in their hats.

Also, despite using decimal.Decimal under the covers it wants its money
as strings.

Python 3.6.3
>>> from money import money
>>> print(money.Money(23.45, 'AUD'))
AUD 23.449999999999999289457264239899814128875732421875
>>> print(money.Money('23.45', 'AUD'))
AUD 23.45
>>> money.CURRENCY['AUD'].decimals
2
>>> money.CURRENCY['AUD'].symbol
'$'
>>> money.CURRENCY['AUD'].name
'Australian Dollar'
>>>

Still thinking about this.

Mike






Melvyn Sopacua

unread,
May 29, 2018, 8:11:39 AM5/29/18
to django...@googlegroups.com

On dinsdag 29 mei 2018 11:03:56 CEST Mike Dewhirst wrote:

 

> Also, despite using decimal.Decimal under the covers it wants its money

> as strings.

>

> Python 3.6.3

>

> >>> from money import money

> >>> print(money.Money(23.45, 'AUD'))

>

> AUD 23.449999999999999289457264239899814128875732421875

>

> >>> print(money.Money('23.45', 'AUD'))

>

> AUD 23.45

 

No, that's decimal.Decimal and floats being an approximation:

 

>>> from decimal import Decimal
>>> Decimal('23.45')
Decimal('23.45')
>>> Decimal(23.45)
Decimal('23.449999999999999289457264239899814128875732421875')

>>> print('{:.48f}'.format(23.45))
23.449999999999999289457264239899814128875732421875
>>> print('{:.55f}'.format(23.45))
23.4499999999999992894572642398998141288757324218750000000

So decimal.Decimal's constructor converts strings to decimal numbers with as much precision as given. It also applies to floats, except that floats have more precision then typed.

--

Melvyn Sopacua

Mike Dewhirst

unread,
May 29, 2018, 7:54:27 PM5/29/18
to django...@googlegroups.com
I get it :)

Thanks (again!)

Mike

> --
>
> Melvyn Sopacua
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/4704533.IngrlRvRmB%40fritzbook
> <https://groups.google.com/d/msgid/django-users/4704533.IngrlRvRmB%40fritzbook?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages