pythondjangodjango-parlerdjango-shop

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.


Solution

  • 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)