---
title: "django-money | pypx"
description: "Adds support for using money and currency fields in django models and forms. Uses py-moneyed as the money implementation."
canonical_url: "https://pypx.app/packages/django-money"
meta:
  "og:description": "Adds support for using money and currency fields in django models and forms. Uses py-moneyed as the money implementation."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-money%2Cversion_3.6.2%2Csummary_Adds%2Bsupport%2Bfor%2Busing%2Bmoney%2Band%2Bcurrency%2Bfields%2Bin%2Bdjango%2Bmodels%2Band%2Bforms.%2BUses%2Bpy-moneyed%2Bas%2Bthe%2Bmoney%2Bimplementation.%2Clicense_BSD%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tbW9uZXki%2Cs_phNnF6UVpdDFhSyZ.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "django-money 3.6.2"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-money%2Cversion_3.6.2%2Csummary_Adds%2Bsupport%2Bfor%2Busing%2Bmoney%2Band%2Bcurrency%2Bfields%2Bin%2Bdjango%2Bmodels%2Band%2Bforms.%2BUses%2Bpy-moneyed%2Bas%2Bthe%2Bmoney%2Bimplementation.%2Clicense_BSD%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tbW9uZXki%2Cs_phNnF6UVpdDFhSyZ.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **django-money**

v3.6.2

Adds support for using money and currency fields in django models and forms. Uses py-moneyed as the money implementation.

37 KB Python 3.10+ py3-none-anyBSD2 deps

[**Diff **](https://pypx.app/packages/django-money/diff)

`$ uv add django-money`

# django-money

![Documentation Status](https://readthedocs.org/projects/django-money/badge/?version=latest) ![PyPI](https://img.shields.io/pypi/v/django-money.svg)

A little Django app that uses [py-moneyed](https://github.com/py-moneyed/py-moneyed)\_ to add support for Money fields in your models and forms.

- Django versions supported: 4.2, 5.0, 5.1, 5.2
- Python versions supported: 3.10, 3.11, 3.12, 3.13
- PyPy versions supported: PyPy3

If you need support for older versions of Django and Python, please refer to older releases mentioned in [the release notes](https://django-money.readthedocs.io/en/latest/changes.html)\_.

Through the dependency [py-moneyed](https://github.com/py-moneyed/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

Using `pip`:

```bash
$ pip install django-money
```

This automatically installs py-moneyed v1.2 (or later).

Add djmoney to your INSTALLED\_APPS. This is required so that money field are displayed correctly in the admin.

```python
INSTALLED_APPS = [
   ...,
   'djmoney',
   ...
]
```

## Model usage

Use as normal model fields:

```python
from djmoney.models.fields import MoneyField
from django.db import models


class BankAccount(models.Model):
    balance = MoneyField(max_digits=14, decimal_places=2, default_currency='USD')
```

To comply with certain strict accounting or financial regulations, you may consider using max\_digits=19 and decimal\_places=4, see more in this [StackOverflow answer](https://stackoverflow.com/a/224866/405682)\_

It is also possible to have a nullable MoneyField:

```python
class BankAccount(models.Model):
    money = MoneyField(max_digits=10, decimal_places=2, null=True, default_currency=None)

account = BankAccount.objects.create()
assert account.money is None
assert account.money_currency is None
```

Searching for models with money fields:

```python
from djmoney.money import Money


account = BankAccount.objects.create(balance=Money(10, 'USD'))
swissAccount = BankAccount.objects.create(balance=Money(10, 'CHF'))

BankAccount.objects.filter(balance__gt=Money(1, 'USD'))
# Returns the "account" object
```

The default currency code length is `3` but you can change it with the `CURRENCY_CODE_MAX_LENGTH` setting.

Caution: this setting also affects the initial migration of the `exchange` plugin, so changing it after running the initial migration has no effect. (You'd need to `manage migrate exchange zero` and migrate again if you want to change it).

## Callable defaults

**Added in version 3.6.0: Callables were introduced in 3.6.0 and did not work before.**

To make things configurable through project settings (especially useful for third-party applications using django-money), you can wrap configurable defaults in a callable. This avoids generating migrations on models when the defaults change!

You can define a [default callable](https://docs.djangoproject.com/en/5.2/ref/models/fields/#default)\_ for your MoneyField defaults, both money and separate currency. Here is an example:

```python
from django.conf import settings
from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.money import Money

def get_default_currency():
    return settings.MY_DEFAULT_CURRENCY

def get_default_zero():
    return Money("0.00", settings.MY_DEFAULT_CURRENCY)

class Product(models.Model):
    # The price has no default value, but a default currency is defined!
    price = MoneyField(max_digits=10, decimals=2, currency_default=get_default_currency)
    # Discount field defaults to 0 Money and thus does not need a default currency (it's in the Money
    # value supplied by get_default_zero
    discount = MoneyField(max_digits=10, decimals=2, default=get_default_zero)
```

Note that the supplied callables are subject to Django's internals and the fact that they need to be serializable for the migration framework.

## Configuring currency choices

It's possible to supply a global Django project setting for CURRENCY\_CHOICES, but you may also supply this as an individual currency\_choices option for MoneyFields:

```python
from django.conf import settings
from django.db import models
from djmoney.models.fields import MoneyField

class Invoice(models.Model):
    # Allow 2 different currencies for total amount
    total_amount = MoneyField(max_digits=15, decimal_places=2, currency_choices=[("CLY", "CLY"), ("USD", "USD")])
    # Only allow 1 currency in the accounted field
    converted_amount = MoneyField(max_digits=15, decimal_places=2, currency_choices=[("CLY", "CLY")])
```

As with other Django Field choices kwargs, you can also supply a callable for currency\_choices (but not a lambda). Please note that - similar to other choices fields - there are NO database constraints implied by this option, so you need may need additional validation safeguards and [adding your own db constraints](https://adamj.eu/tech/2020/01/22/djangos-field-choices-dont-constrain-your-data/)\_.

## Field validation

There are 3 different possibilities for field validation:

- by numeric part of money despite on currency;
- by single money amount;
- by multiple money amounts.

All of them could be used in a combination as is shown below:

```python
from django.db import models
from djmoney.models.fields import MoneyField
from djmoney.money import Money
from djmoney.models.validators import MaxMoneyValidator, MinMoneyValidator


class BankAccount(models.Model):
    balance = MoneyField(
        max_digits=10,
        decimal_places=2,
        validators=[
            MinMoneyValidator(10),
            MaxMoneyValidator(1500),
            MinMoneyValidator(Money(500, 'NOK')),
            MaxMoneyValidator(Money(900, 'NOK')),
            MinMoneyValidator({'EUR': 100, 'USD': 50}),
            MaxMoneyValidator({'EUR': 1000, 'USD': 500}),
        ]
    )
```

The balance field from the model above has the following validation:

- All input values should be between 10 and 1500 despite on currency;
- Norwegian Crowns amount (NOK) should be between 500 and 900;
- Euros should be between 100 and 1000;
- US Dollars should be between 50 and 500;

## Constructing form data

The default ModelForm class will use a form field (djmoney.forms.fields.MoneyField) that is constructed of two separate fields for amount and currency.

If you need to feed data directly to such a form (for instance if you are writing a test case), then you need to pass amount and currency like this:

```python
# models.py
class Product(models.Model):
    price = MoneyField(
        max_digits=14,
        decimal_places=2,
        default_currency='EUR'
    )

# forms.py
class ProductForm(ModelForm):
    class Meta:
        model = Product
        fields = ["price"]

# tests.py

# construct the form in your test case
form = ProductForm({'price_0': 10, 'price_1': 'EUR'})
```

## 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 simply add these few lines to your settings.py file:

```python
import moneyed

BOB = moneyed.add_currency(
    code='BOB',
    numeric='068',
    name='Peso boliviano',
    countries=('BOLIVIA', )
)
```

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

```python
CURRENCIES = ('USD', 'BOB')
```

**The list has to contain valid Currency codes**

Additionally there is an ability to specify currency choices directly:

```python
CURRENCIES = ('USD', 'EUR')
CURRENCY_CHOICES = [('USD', 'USD $'), ('EUR', 'EUR €')]
```

## 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:

```python
from djmoney.models.managers import money_manager


class BankAccount(models.Model):
    balance = MoneyField(max_digits=10, decimal_places=2, default_currency='USD')
    accounts = money_manager(MyCustomManager())
```

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:

```python
from djmoney.models.managers import understands_money


class MyCustomQuerySet(QuerySet):

   @understands_money
   def my_custom_method(*args, **kwargs):
       # Awesome stuff
```

## Note on serialization

Django-money provides a custom deserializer, it is not registered by default so you will have to actively register it in your settings.py.

```python
SERIALIZATION_MODULES = {"json": "djmoney.serializers"}
```

## 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:

```python
INSTALLED_APPS += ('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 ".[test]" # installation with required packages for testing
```

Recommended way to run the tests:

```bash
tox
```

Testing the application in the current environment python:

```bash
make test
```

## Working with Exchange Rates

To work with exchange rates, add the following to your INSTALLED\_APPS.

```python
INSTALLED_APPS = [
    ...,
    'djmoney.contrib.exchange',
]
```

Also, it is required to have certifi installed. It could be done via installing djmoney with exchange extra:

```bash
pip install "django-money[exchange]"
```

To create required relations run python manage.py migrate. To fill these relations with data you need to choose a data source. Currently, 2 data sources are supported - https://openexchangerates.org/ (default) and https://fixer.io/. To choose another data source set EXCHANGE\_BACKEND settings with importable string to the backend you need:

```python
EXCHANGE_BACKEND = 'djmoney.contrib.exchange.backends.FixerBackend'
```

If you want to implement your own backend, you need to extend djmoney.contrib.exchange.backends.base.BaseExchangeBackend. Two data sources mentioned above are not open, so you have to specify access keys in order to use them:

OPEN\_EXCHANGE\_RATES\_APP\_ID - '\<your actual key from openexchangerates.org>'

FIXER\_ACCESS\_KEY - '\<your actual key from fixer.io>'

Backends return rates for a base currency, by default it is USD, but could be changed via BASE\_CURRENCY setting. Open Exchanger Rates & Fixer supports some extra stuff, like historical data or restricting currencies in responses to the certain list. In order to use these features you could change default URLs for these backends:

```python
OPEN_EXCHANGE_RATES_URL = 'https://openexchangerates.org/api/historical/2017-01-01.json?symbols=EUR,NOK,SEK,CZK'
FIXER_URL = 'http://data.fixer.io/api/2013-12-24?symbols=EUR,NOK,SEK,CZK'
```

Or, you could pass it directly to update\_rates method:

```python
>>> from djmoney.contrib.exchange.backends import OpenExchangeRatesBackend
>>> backend = OpenExchangeRatesBackend(url='https://openexchangerates.org/api/historical/2017-01-01.json')
>>> backend.update_rates(symbols='EUR,NOK,SEK,CZK')
```

There is a possibility to use multiple backends in the same time:

```python
>>> from djmoney.contrib.exchange.backends import FixerBackend, OpenExchangeRatesBackend
>>> from djmoney.contrib.exchange.models import get_rate
>>> OpenExchangeRatesBackend().update_rates()
>>> FixerBackend().update_rates()
>>> get_rate('USD', 'EUR', backend=OpenExchangeRatesBackend.name)
>>> get_rate('USD', 'EUR', backend=FixerBackend.name)
```

Regular operations with Money will use EXCHANGE\_BACKEND backend to get the rates. Also, there are two management commands for updating rates and removing them:

```bash
$ python manage.py update_rates
Successfully updated rates from openexchangerates.org
$ python manage.py clear_rates
Successfully cleared rates for openexchangerates.org
```

Both of them accept -b/--backend option, that will update/clear data only for this backend. And clear\_rates accepts -a/--all option, that will clear data for all backends.

To set up a periodic rates update you could use Celery task:

```python
CELERY_BEAT_SCHEDULE = {
    'update_rates': {
        'task': 'path.to.your.task',
        'schedule': crontab(minute=0, hour=0),
        'kwargs': {}  # For custom arguments
    }
}
```

Example task implementation:

```python
from django.utils.module_loading import import_string

from celery import Celery
from djmoney import settings


app = Celery('tasks', broker='pyamqp://guest@localhost//')


@app.task
def update_rates(backend=settings.EXCHANGE_BACKEND, **kwargs):
    backend = import_string(backend)()
    backend.update_rates(**kwargs)
```

To convert one currency to another:

```python
>>> from djmoney.money import Money
>>> from djmoney.contrib.exchange.models import convert_money
>>> convert_money(Money(100, 'EUR'), 'USD')
<Money: 122.8184375038380800 USD>
```

Exchange rates are integrated with Django Admin.

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 and is in the INSTALLED\_APPS of your settings.py and that rest\_framework has been installed. MoneyField will automatically register a serializer for Django REST Framework through djmoney.apps.MoneyConfig.ready().

You can add a serializable field the following way:

```python
from djmoney.contrib.django_rest_framework import MoneyField

class Serializers(serializers.Serializer):
    my_computed_prop = MoneyField(max_digits=10, decimal_places=2)
```

Built-in serializer works in the following way:

```python
class Expenses(models.Model):
    amount = MoneyField(max_digits=10, decimal_places=2)


class Serializer(serializers.ModelSerializer):
    class Meta:
        model = Expenses
        fields = '__all__'

>>> instance = Expenses.objects.create(amount=Money(10, 'EUR'))
>>> serializer = Serializer(instance=instance)
>>> serializer.data
ReturnDict([
    ('id', 1),
    ('amount_currency', 'EUR'),
    ('amount', '10.000'),
])
```

Note that when specifying individual fields on your serializer, the amount and currency fields are treated separately. To achieve the same behaviour as above you would include both field names:

```python
class Serializer(serializers.ModelSerializer):
    class Meta:
        model = Expenses
        fields = ('id', 'amount', 'amount_currency')
```

## Customization

If there is a need to customize the process deconstructing Money instances onto Django Fields and the other way around, then it is possible to use a custom descriptor like this one:

```python
class MyMoneyDescriptor:

    def __get__(self, obj, type=None):
        amount = obj.__dict__[self.field.name]
        return Money(amount, "EUR")
```

It will always use EUR for all Money instances when obj.money is called. Then it should be passed to MoneyField:

```python
class Expenses(models.Model):
    amount = MoneyField(max_digits=10, decimal_places=2, money_descriptor_class=MyMoneyDescriptor)
```

## Background

This project is a 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.

## **Details **

<dl>

<dt>Version</dt>
<dd>3.6.2</dd>

<dt>License</dt>
<dd>BSD</dd>

<dt>Python</dt>
<dd>&gt;=3.10</dd>

</dl>

## **Links**

- [Homepage](https://codeberg.org/django-money/django-money)
- [View on PyPI](https://pypi.org/project/django-money/)

## **Release Cadence **

**3**

releases in the past year

avg 69 days between releases

## **Maintainers **

- Greg Reinbach · [greg@reinbach.com](mailto:greg@reinbach.com)
- Benjamin Balder Bach · [benjamin@overtag.dk](mailto:benjamin@overtag.dk)
- Dmitry Dygalo · [dmitry@dygalo.dev](mailto:dmitry@dygalo.dev)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/django-money#webpage","@type":"WebPage","description":"Adds support for using money and currency fields in django models and forms. Uses py-moneyed as the money implementation.","name":"django-money","url":"https://pypx.app/packages/django-money","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/django-money"]}]},{"@id":"https://pypx.app/packages/django-money#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Adds support for using money and currency fields in django models and forms. Uses py-moneyed as the money implementation.","license":"BSD","name":"django-money","softwareVersion":"3.6.2"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
