Code Monkey home page Code Monkey logo

django-money's Introduction

Django-money

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.

Via py-moneyed, django-moneyed 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

Clone and run the setup script:

git clone https://[email protected]/jakewins/django-money.git
cd django-money
python setup.py install

Model usage

Use as normal model fields

import moneyed
from djmoney.models.fields import MoneyField
from django.db import models

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

Searching for models with money fields:

from moneyed import Money, USD, CHF
account = BankAccount(balance=Money(10, USD))
swissAccount = BankAccount(balance=Money(10, CHF))

account.save()
swissAccount.save()

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

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:

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:

from djmoney.models.managers import understand_money

class MyCustomQuerySet(QuerySet):

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

django-money's People

Contributors

akumria avatar benjaoming avatar dekkers avatar gcallaghan avatar hank avatar jakewins avatar rach avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.