class MoneyModelDefaultMoneyUSD(models.Model): name = models.CharField(max_length=100) price = MoneyField(max_digits=12, decimal_places=3, default=Money('123.45', 'USD')) zero = MoneyField(max_digits=12, decimal_places=3, default=Money('0', 'USD')) def __str__(self): return self.name + ' ' + str(self.price)
def model_from_db_view(request, amount='0', currency=settings.BASE_CURRENCY): # db roundtrip instance = SimpleMoneyModel.objects.create(price=Money(amount, currency)) instance = SimpleMoneyModel.objects.get(pk=instance.pk) money = instance.price return render_to_response('view.html', {'money': money})
class ParametrizedDefaultAsMoneyModel(models.Model): """ The simplest possible declaration with a Money object """ value = MoneyField(max_digits=12, decimal_places=3, default=Money(100, 'JPY')) @staticmethod def expected_value(): return Money('100', 'JPY')
def to_internal_value(self, value): """ Validate that the input is a Money accepted number and return a Money instance. """ try: value = Money(value) except IncorrectMoneyInputError: self.fail('invalid') return self.validate_precision(value)
def test_money_field_serializer(self): model = SimpleMoneyModel.objects.create(name='Test', price=Money(amount='100.00', currency='EUR')) serializer = SimpleMoneyModelSerializer(model) expected = { 'id': model.pk, 'name': model.name, 'price': model.price.amount_rounded, 'price_currency': model.price.currency, } assert serializer.data == expected
def instance_view(request): money = Money('0.0', 'JPY') return render_to_response('view.html', {'money': money})
def model_view(request): instance = SimpleMoneyModel(price=Money('0.0', 'JPY')) money = instance.price return render_to_response('view.html', {'money': money})
def expected_value(): return Money('0', settings.BASE_CURRENCY)
def expected_value(): return Money('100', 'JPY')