![]() This project is a fork of the Django support that was in Model ): amount = MoneyField ( max_digits = 10, decimal_places = 2, money_descriptor_class = MyMoneyDescriptor ) Background Then it should be passed to MoneyField: class Expenses ( models. It will always use EUR for all Money instances when obj.money is called. Then it is possible to use a custom descriptor like this one: class MyMoneyDescriptor : def _get_ ( self, obj, type = None ): amount = obj. If there is a need to customize the process deconstructing Money instances onto Django Fields and the other way around, ModelSerializer ): class Meta : model = Expenses fields = ( 'id', 'amount', 'amount_currency' ) Customization ![]() To achieve the same behaviour as above you would include both field names: class Serializer ( serializers. Note that when specifying individual fields on your serializer, the amount and currency fields are treated separately. ![]() create ( amount = Money ( 10, 'EUR' )) > serializer = Serializer ( instance = instance ) > serializer. ModelSerializer ): class Meta : model = Expenses fields = '_all_' > instance = Expenses. Model ): amount = MoneyField ( max_digits = 10, decimal_places = 2 ) class Serializer ( serializers. Serializer ): my_computed_prop = MoneyField ( max_digits = 10, decimal_places = 2 )īuilt-in serializer works in the following way: class Expenses ( models. You can add a serializable field the following way: from _rest_framework import MoneyField class Serializers ( serializers. ![]() MoneyField willĪutomatically register a serializer for Django REST Framework through Settings.py and that rest_framework has been installed. Make sure that djmoney and is in the INSTALLED_APPS of your You lose some properties like commutativity ( A + B = B + A) due toĬonversions happening in different directions. Occurs, and automatic conversion can hide bugs. Use cases you will need to be clear about exactly when currency conversion Note that currency conversion is a lossy process, so automaticĬonversion is usually a good strategy only for very simple use cases. To convert one currency to another: > from djmoney.money import Money > from import convert_money > convert_money ( Money ( 100, 'EUR' ), 'USD' ) Įxchange rates are integrated with Django Admin.ĭjango-money can be configured to automatically use this app for currencyĬonversions by settings AUTO_CONVERT_MONEY = True in your Django EXCHANGE_BACKEND, ** kwargs ): backend = import_string ( backend )() backend. task def update_rates ( backend = settings. ![]() PyPy versions supported: PyP圓 (for Django %}Įxample task implementation: from _loading import import_string from celery import Celery from djmoney import settings app = Celery ( 'tasks', broker = ). A little Django app that uses py-moneyed to add support for Moneyĭjango versions supported: 2.2, 3.2, 4.0, 4.1, 4.2 ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |