跳转到主要内容

价格模块的Django字段

项目描述

django-prices: Django 的 prices 模块字段

Build Status codecov.io

安装

  • pip install django-prices
  • django_prices 添加到 settings.py 中的 INSTALLED_APPS
  • 遵循 enmerkar说明 并更新你的 INSTALLED_APPSMIDDLEWARE_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 版本对模型中价格数据的存储方式进行了重大更改,允许按模型实例设置价格货币。

迁移步骤

  1. 在你的 表单

    • 移除 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)
    
  2. 在你的使用 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)
      
  3. 在你的 迁移 文件中

    • 将所有 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')
      
  4. 重命名模型中的字段。你的旧字段仍然会存储货币金额,所以最好的选择可能是 price_net_amount 而不是 price_net

  5. 所有使用模型及其字段的地点都可以防止 django 应用启动代码。可能的问题:代码尝试访问不存在的字段。目前从你的 ModelForms、Graphene 类型等中排除这些字段。

  6. 运行 python manage.py makemigrations。确保在添加新的 MoneyFields 到模型之前执行此步骤!如果不这样做,django 将生成 delete/create 迁移而不是 rename

  7. 运行 python manage.py migrate

  8. 更新 django-prices

  9. 在你的模型中添加 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")
    
  10. 运行 python manage.py makemigrationspython manage.py migrate

  11. 更改 TaxedMoneyField 声明

    price = TaxedMoneyField(
        net_amount_field="price_net_amount",
        gross_amount_field="price_gross_amount",
        currency="currency",
    )
    
  12. 请记住处理先前编辑的 ModelForms 中的更改

项目详情


发布历史 发布通知 | RSS 源

下载文件

下载适用于您平台的文件。如果您不确定选择哪一个,请了解更多关于安装包的信息。

源代码分布

django-prices-2.3.0.tar.gz (10.5 kB 查看哈希值)

上传时间 源代码

构建分布

django_prices-2.3.0-py3-none-any.whl (10.4 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面