The boring way: add an extra field to the Item class called priceUSD and fill it out using the save method. means that you have no load on performing calculations for each request - only for each update. Is this so good or will it not depend on whether you want to write more or read more (given your question, maybe he reads?)
something like that:
class Item(models.Model): priceRT = models.DecimalField(max_digits=15, decimal_places=2, default=0) cur = models.ForeignKey(Currency) priceUSD = models.DecimalField(max_digits=15, decimal_places=2, default=0) def save(self,*args,**kwargs) self.priceUSD = self.priceRT * self.cur.rateToUSD super(Model,self).save(*args,**kwargs)
In my django material, when I tried to implement smart computed fields without storing them in the database, I usually found that it has too many flaws to use usefult (for example, it cannot query them, sorting by them). therefore, saving the correct field in the database using a special save method is how I usually did it, and it works fine. you will need a little more error checking, etc.
source share