2016-09-17 49 views
0

我使用django-money來表示我的項目中的價格信息。Django-money:decimal_places不起作用

我不想表現出任何小數點,所以我設置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,但它也顯示了兩個小點。

感謝。

回答

0

由於版本0.10.2 django-money有配置小數位輸出的功能。

在您的django設置模塊中將CURRENCY_DECIMAL_PLACES設置爲0,它應該按照您的需要工作。