Atterratio
Atterratio

Reputation: 466

What the right way to localize the price in the Django-shop?

I know easy way, make a few different fields for needed currencies, but that's not only ugly, but the currencies will be hardcoded. It seems to me be more elegant through django-parler, but I do not quite understand how to do it.

Upvotes: 1

Views: 320

Answers (2)

jrief
jrief

Reputation: 1695

The simplest way to localize prices in django-SHOP, is to use the MoneyInXXX class. This class can be generated for each currency using the MoneyMaker factory.

Whenever an amount of a Money class is formatted, it is localized properly.

Upvotes: 1

Atterratio
Atterratio

Reputation: 466

I think this is the right way:

class CurrencyModel(TranslatableModel):
    translations = TranslatedFields(
        title = models.CharField(_("Title"), max_length=120),
    )
    code = models.CharField(_('ISO 4217 code'), max_lenght=3)

    def __str__(self):
        return self.title

class ItemModel(BaseProduct, TranslatableModel):
    slug = models.SlugField(_("Slug"), unique=True)
    translations = TranslatedFields(
        product_name = models.CharField(_("Item Name"), max_length=256),
        item_price = models.FloatField(_("Item price")),
        currency = models.ForeignKey(CurrencyModel, verbose_name=_("Currency ")),
    )

    def get_price(self, request):
        money = MoneyMaker(self.currency.code)
        return money(self.item_price)

Upvotes: 2

Related Questions