Skip to content

django4life/django-money

 
 

Repository files navigation

Django-money

Build Status

Coverage Status

Documentation Status

PyPI

A little Django app that uses py-moneyed to add support for Money fields in your models and forms.

Fork of the Django support that was in http://code.google.com/p/python-money/

This version adds tests, and comes with several critical bugfixes.

Django versions supported: 1.8, 1.10, 1.11

Python versions supported: 2.7, 3.3, 3.4, 3.5, 3.6

PyPy versions supported: PyPy 2.6, PyPy3 2.4

If you need support for older versions of Django and Python you can use the latest version in 0.11.x branch.

Via py-moneyed, django-money gets:

  • Support for proper Money value handling (using the standard Money design pattern)
  • A currency class and definitions for all currencies in circulation
  • Formatting of most currencies with correct currency sign

Installation

Django-money currently needs py-moneyed v0.7 (or later) to work.

You can obtain the source code for django-money from here:

https://github.com/django-money/django-money

And the source for py-moneyed from here:

https://github.com/limist/py-moneyed

Using `pip`:

pip install py-moneyed django-money

Model usage

Use as normal model fields

Searching for models with money fields:

Adding a new Currency

Currencies are listed on moneyed, and this modules use this to provide a choice list on the admin, also for validation.

To add a new currency available on all the project, you can simple add this two lines on your settings.py file

To restrict the currencies listed on the project set a CURRENCIES variable with a list of Currency codes on settings.py

The list has to contain valid Currency codes

Additionally there is an ability to specify currency choices directly:

Important note on model managers

Django-money leaves you to use any custom model managers you like for your models, but it needs to wrap some of the methods to allow searching for models with money values.

This is done automatically for the "objects" attribute in any model that uses MoneyField. However, if you assign managers to some other attribute, you have to wrap your manager manually, like so:

Also, the money_manager wrapper only wraps the standard QuerySet methods. If you define custom QuerySet methods, that do not end up using any of the standard ones (like "get", "filter" and so on), then you also need to manually decorate those custom methods, like so:

Format localization

The formatting is turned on if you have set USE_L10N = True in the your settings file.

If formatting is disabled in the configuration, then in the templates will be used default formatting.

In the templates you can use a special tag to format the money.

In the file settings.py add to INSTALLED_APPS entry from the library djmoney:

In the template, add:

{% load djmoney %}
...
{% money_localize money %}

and that is all.

Instructions to the tag money_localize:

{% money_localize <money_object> [ on(default) | off ] [as var_name] %}
{% money_localize <amount> <currency> [ on(default) | off ] [as var_name] %}

Examples:

The same effect:

{% money_localize money_object %}
{% money_localize money_object on %}

Assignment to a variable:

{% money_localize money_object on as NEW_MONEY_OBJECT %}

Formatting the number with currency:

{% money_localize '4.5' 'USD' %}
Return::

    Money object

Testing

Install the required packages:

git clone https://github.com/django-money/django-money

cd ./django-money/

pip install -e .[tests] # installation with required packages for testing

Recommended way to run the tests:

Testing the application in the current environment python:

Working with Exchange Rates

To work with exchange rates, check out this repo that builds off of django-money: https://github.com/evonove/django-money-rates

django-money can be configured to automatically use this app for currency conversions by settings AUTO_CONVERT_MONEY = True in your Django settings. Note that currency conversion is a lossy process, so automatic conversion is usually a good strategy only for very simple use cases. For most use cases you will need to be clear about exactly when currency conversion occurs, and automatic conversion can hide bugs. Also, with automatic conversion you lose some properties like commutativity (A + B == B + A) due to conversions happening in different directions.

Usage with Django REST Framework

Make sure that djmoney is in the INSTALLED_APPS of your settings.py and MoneyFields to automatically work with Django REST Framework.

Built-in serializer works in the following way:

Known Issues

Updates to a model form will not save in Django 1.10.1. They will save in 1.10.0 and is expected to be fixed in Django 1.10.2. :

https://github.com/django/django/pull/7217

About

Money fields for django forms and models.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 98.9%
  • Makefile 1.1%