首页 > 解决方案 > Django (djmoney):如何从 MoneyField 获取原始值?

问题描述

我正在使用这个包来装饰我的货币领域: https ://github.com/django-money/django-money

我有一个模态字段:

client_cost = MoneyField(
        _('client cost'),
        max_digits=10,
        decimal_places=2,
        default_currency='AUD',
        null=True,
    )

现在我想对其进行一些计算,因此我将其转换为 int 使用int(client_cost) 但是我收到一个错误(显然):

int() argument must be a string, a bytes-like object or a number, not 'Money'

请帮忙。

标签: pythondjango

解决方案


您正在处理一个Money对象(请参见此处)。该对象有一个amount属性,可以给你小数:

>>> client_cost.amount
Decimal('19.50')

int如果你愿意,你可以转换成它。


推荐阅读