I have a Product model which has a price field and a currency field. the user can save different products using different currencies. some products are saved in usd, some in gbp, etc'..
class Product(models.Model):
price = models.DecimalField(
decimal_places=1,
max_digits=4,
default=0
)
usd = 'USD'
nis = 'NIS'
gbp = 'GBP'
CURRENCY_CHOICES = (
(usd , 'USD'),
(nis, 'NIS'),
(gbp, 'GBP')
)
currency = models.CharField(
max_length=3,
choices=CURRENCY_CHOICES,
default=usd,
blank=True
)
I want to be able to sort and view all of the products in a single currency.
how can i add a field, price_in_usd
, which will be set automatically when fields 'price' and 'currency' will be set?
something like this for example:
price_in_usd = convert_price_to_usd()
convert_price_to_usd():
if currency == GPB:
return price*1.25
if currency == NIS:
return price*0.33