Django QuerySet ordering by expression

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2010-05-28T06:01:52Z Indexed on 2010/05/28 6:11 UTC
Read the original article Hit count: 287

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about django

Related posts about orm