Package: django-moneyfield
Django model field for monetary amounts.Installationpip install django-moneyfieldMoneyfield requires:Python ==3.3Django ==1.5 (still working on 1.6 compatibility)MoneyBabel(if you need currency formatting)Basic usagefromdjango.dbimportModelsfrommoneyfieldimportMoneyFieldclassBook(models.Model):name=models.CharField(blank=True,max_length=100)price=MoneyField(decimal_places=2,max_digits=8)The fieldpricewill be created in the database as two columns:price_amount, andprice_currency. You may use any name<fieldname>, resulting in columns<fieldname>_amountand<fieldname>_currency.CREATETABLE"myapp_book"("id"integerNOTNULLPRIMARYKEY,"name"varchar(100)NOTNULL,"price_amount"decimalNOTNULL,"price_currency"varchar(3)NOTNULL);The attributepriceis only a convenience python descriptor that accepts and returns Money objects, and will be available only when working with a model instance.>>>book=Book.objects.get(id=1)>>>book.priceUSD19.99>>>book.price=Money("9.99","USD")>>>book.save()>>>book.priceUSD9.99For any operation using Managers and QuerySets, the amount and the currency must be addressed separately, usingprice_amountandprice_currencyin this case. This allows for maximum flexibility and unambiguity.new_book=Book.objects.create(name="The new book",price_amount=Decimal("29.99"),price_currency="USD")books_in_usd=Book.objects.filter(price_currency="USD")cheap_books=Book.objects.filter(price_amount__lt=Decimal('2'))cheap_books_eur=Book.objects.filter(price_amount__lt=Decimal('2'),price_currency="EUR")Defaults and choicesYou can provide separate defaults for the amount and the currency as Decimal and the three letter currency code string, respectively:classBook(models.Model):name=models.CharField(blank=True,max_length=100)price=MoneyField(decimal_places=2,max_digits=8,amount_default=Decimal("0"),currency_default="USD")or a default Money value:classBook(models.Model):name=models.CharField(blank=True,max_length=100)price=MoneyField(decimal_places=2,max_digits=8,default=Money("0","USD"))You can also set currency choices withcurrency_choicesand a currency default withcurrency_default:classBook(models.Model):CURRENCY_CHOICES=(('EUR','EUR'),('USD','USD'))CURRENCY_DEFAULT='EUR'name=models.CharField(blank=True,max_length=100)price=MoneyField(decimal_places=2,max_digits=8,currency_choices=CURRENCY_CHOICES,currency_default=CURRENCY_DEFAULT)Fixed currencyIf you don’t need to handle different currencies but want to benefit from using the Money class instead of just Decimals, you may want to set a fixed currency for your monetary field:classBook(models.Model):name=models.CharField(blank=True,max_length=100)price=MoneyField(decimal_places=2,max_digits=12,currency='USD')In this case, the attributepricewill only accept and return Money objects with currency “USD”.The database representation of this field will beprice_amount,with no currency column. This is consistent with the multi-currency case, and allows for maximum flexibility while making schema migrations.MoneyField optionsMoneyField.max_digitsSame as DecimalField: The maximum number of digits allowed in the number. Note that this number must be greater than or equal todecimal_places.MoneyField.decimal_placesSame as DecimalField: The number of decimal places to store with the number.MoneyField.currencyFixed currency for this field. This will omit the creation of a<name>_currencycolumn in the database.MoneyField.defaultDefault Money value for this field (both amount and currency).MoneyField.currency_defaultDefault currency value.MoneyField.amount_defaultDefault amount value.MoneyField.currency_choicesRegular Django choices iterable, e.g.:CURRENCY_CHOICES = (
    ('EUR', 'Euros'),
    ('USD', 'US Dollars')
)FormsA base model form classMoneyModelFormis included to show the monetary fields as just one field in forms, instead of separate amount and currency fields.fromdjango.contribimportadminfrommoneyfieldimportMoneyModelFormfrommyapp.modelsimportBookclassBookAdmin(admin.ModelAdmin):list_display=['id','name','price']form=MoneyModelFormadmin.site.register(Book,BookAdmin)UsingMoneyModelFormis optional. You may also include it in the base classes of your custom model form class.Using currency choicesUsing fixed currencyUsing free currencyContributionsContributions are welcome. You can use theregular github mechanisms.To run the tests, sit on the package root (by setup.py) and run:python tests/runtests.pyLicensedjango-moneyfield is released under theMIT license, which can be found in the fileLICENSE.