价格模块的Django字段
项目描述
django-prices: Django 的 prices 模块字段
安装
- pip install django-prices
- 将 django_prices添加到settings.py中的INSTALLED_APPS
- 遵循 enmerkar的 说明 并更新你的INSTALLED_APPS和MIDDLEWARE_CLASSES。
功能
提供对模型的支撑
from django.db import models
from django_prices.models import MoneyField, TaxedMoneyField
class Model(models.Model):
    currency = models.CharField(max_length=3, default="BTC")
    price_net_amount = models.DecimalField(max_digits=9, decimal_places=2, default="5")
    price_net = MoneyField(amount_field="price_net_amount", currency_field="currency")
    price_gross_amount = models.DecimalField(
        max_digits=9, decimal_places=2, default="5"
    )
    price_gross = MoneyField(
        amount_field="price_gross_amount", currency_field="currency"
    )
    price = TaxedMoneyField(
        net_amount_field="price_net_amount",
        gross_amount_field="price_gross_amount",
        currency="currency",
    )
和对表单的支撑
from django import forms
from django_prices.forms import MoneyField
AVAILABLE_CURRENCIES = ["BTC", "USD"]
class ProductForm(forms.Form):
    name = forms.CharField(label="Name")
    price_net = MoneyField(label="net", available_currencies=AVAILABLE_CURRENCIES)
以及对验证器的支撑
from django import forms
from prices.forms import Money
from django_prices.forms import MoneyField
from django_prices.validators import (
    MaxMoneyValidator, MinMoneyValidator, MoneyPrecisionValidator)
class DonateForm(forms.Form):
    donator_name = forms.CharField(label="Your name")
    donation = MoneyField(
        label="net",
        available_currencies=["BTC", "USD"],
        max_digits=9,
        decimal_places=2,
        validators=[
            MoneyPrecisionValidator(),
            MinMoneyValidator(Money(5, "USD")),
            MaxMoneyValidator(Money(500, "USD")),
        ],
    )
它还提供对模板的支撑
{% load prices %}
<p>Price: {{ foo.price.gross|amount }} ({{ foo.price.net|amount }} + {{ foo.price.tax|amount }} tax)</p>
你可以使用 prices 模板标签的 HTML 输出,它们会在 <span> 元素中包裹货币符号
{% load prices %}
<p>Price: {{ foo.price.gross|amount:'html' }} ({{ foo.price.net|amount:'html' }} + {{ foo.price.tax|amount:'html' }} tax)</p>
渲染结构如下(例如使用英语区域设置)
<span class="currency">$</span>15.00
如何迁移到 django-prices 2.0
2.0 版本对模型中价格数据的存储方式进行了重大更改,允许按模型实例设置价格货币。
迁移步骤
- 
在你的 表单 中 - 移除 currency参数
- 添加 available_currencies并指定可用选项。
 如果表单在 fields选项中指定了MoneyFields,则用显式声明替换它们AVAILABLE_CURRENCIES = [("BTC", "bitcoins"), ("USD", "US dollar")] class ModelForm(forms.ModelForm): class Meta: model = models.Model fields = [] price_net = MoneyField(available_currencies=AVAILABLE_CURRENCIES) 
- 移除 
- 
在你的使用 MoneyField的 模型 中- 
将所有 MoneyField类的实例替换为DecimalField
- 
从它们中移除 currency参数
- 
将 default从 Money 实例更改为 Decimal 字段可接受的价值代码示例 price_net = MoneyField( "net", currency="BTC", default=Money("5", "BTC"), max_digits=9, decimal_places=2 ) 更新后的代码 price_net = models.DecimalField("net", default="5", max_digits=9, decimal_places=2) 
 
- 
- 
在你的 迁移 文件中 - 
将所有 MoneyField类的实例替换为DecimalField
- 
从它们中移除 currency参数
- 
将 default从 Money 实例更改为 Decimal 字段可接受的价值field = django_prices.models.MoneyField(currency='BTC', decimal_places=2, default='5', max_digits=9, verbose_name='net') 更新后的代码 field = models.DecimalField(decimal_places=2, default='5', max_digits=9, verbose_name='net') 
 
- 
- 
重命名模型中的字段。你的旧字段仍然会存储货币金额,所以最好的选择可能是 price_net_amount而不是price_net。
- 
所有使用模型及其字段的地点都可以防止 django 应用启动代码。可能的问题:代码尝试访问不存在的字段。目前从你的 ModelForms、Graphene 类型等中排除这些字段。 
- 
运行 python manage.py makemigrations。确保在添加新的MoneyFields到模型之前执行此步骤!如果不这样做,django 将生成delete/create迁移而不是rename。
- 
运行 python manage.py migrate。
- 
更新 django-prices。
- 
在你的模型中添加 models.CharField对货币和MoneyField的引用currency = models.CharField(max_length=3, default="BTC") price_net_amount = models.DecimalField("net", default="5", max_digits=9, decimal_places=2) price_net = MoneyField(amount_field="price_net_amount", currency_field="currency") 
- 
运行 python manage.py makemigrations和python manage.py migrate。
- 
更改 TaxedMoneyField声明price = TaxedMoneyField( net_amount_field="price_net_amount", gross_amount_field="price_gross_amount", currency="currency", ) 
- 
请记住处理先前编辑的 ModelForms 中的更改 
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。