Django QuerySet ordering by expression
- by Andrew
How can i use order_by like order_by('field1'*'field2')
For example i have items with price listed in different currencies, so to order items - i have to make currency conversion.
class Currency(models.Model):
code = models.CharField(max_length=3, primary_key=True)
rateToUSD = models.DecimalField(max_digits=20,decimal_places=10)
class Item(models.Model):
priceRT = models.DecimalField(max_digits=15, decimal_places=2, default=0)
cur = models.ForeignKey(Currency)
I would like to have something like:
Item.objects.all().order_by(F('priceRT')*F('cur__rateToUSD'))
But unfortunately it doesnt work, i also faild with annotate.
How can i permorm QuerySet ordering by result of value multiplication of 2 model's fields.