Django-money:decimal_places不起作用

时间:2016-09-17 22:27:11

标签: django

我使用django-money代表我的项目中的价格信息。

我不想显示任何小数点,所以我将decimal_places设置为零,但它始终显示两个小数点(.00

这是代码。

models.py

class Product(TimeStampedModel):
    name = models.CharField(max_length=120)
    slug = models.SlugField(null=True, blank=True)
    description = models.CharField(max_length=400, blank=True)
    is_active = models.BooleanField(default=True)

    objects = ProductManager()

    class Meta:
        ordering = ('-created',)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse(
            "products:product_detail",
            kwargs={
                "slug": self.slug,
            }
        )


class Variation(TimeStampedModel):
    COLOR_CHOICES = (
        ('black', '흑백'),
        ('single', '단색'),
        ('multi', '컬러'),
    )
    price = MoneyField(max_digits=15, decimal_places=0, default_currency='USD')
    product = models.ForeignKey(Product)
    color = models.CharField(
        max_length=10,
        choices=COLOR_CHOICES,
        default='흑백',
        unique=True,
    )
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return self.get_color_display()

signals.py

@receiver(post_save, sender=Variation)
def post_save_variation(sender, instance, created, **kwargs):
    if created:
        if instance.color == 'black':
            instance.price = Money(40000, USD)
        elif instance.color == 'single':
            instance.price = Money(50000, USD)
        elif instance.color == 'multi':
            instance.price = Money(60000, USD)
        instance.save()

product_detail.html

{% extends "base.html" %}
{% load djmoney %}


{% block content %}
    <h2> Product Detail 페이지입니다!</h2>
    <p> {{ product.name }}</p>
    <p> {{ product.description }}</p>


    컬러:
    <select class="form-contorl">
        {% for variation in product.variation_set.all %}
            <option value="{{ variation.color }}"> {{ variation }} </option>
        {% endfor %}
    </select>

    {% money_localize product.variation_set.last.price %}
{% endblock %}

enter image description here

我该如何处理?我也使用KRW,但它也显示了两个deicmal points。

感谢。

1 个答案:

答案 0 :(得分:0)

由于版本0.10.2,django-money具有配置小数位输出的能力。

在django设置模块中将CURRENCY_DECIMAL_PLACES设置为0,它应该可以按照您的需要工作。