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

291 views Asked by At

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.

2

There are 2 answers

0
Atterratio On BEST ANSWER

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)
0
jrief On

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.