rewrite PostgreSQL query to Django ORM

36 views
Skip to first unread message

Artem Bernatskyy

unread,
Nov 29, 2016, 4:07:42 PM11/29/16
to Django users
Hello, how to rewrite this PostgreSQL query to Django ORM with no raw SQL ?


select * from finance_fund order by((select price from finance_nav where date='2016-11-08' and fund_id=finance_fund.id)/(select price from finance_nav where date='2016-11-07' and fund_id=finance_fund.id)) LIMIT 30;

So basically i need to order by division result of two simple queries of backward relations of Foreign key

Thanks in advance !

Avraham Serour

unread,
Nov 29, 2016, 9:09:47 PM11/29/16
to django-users
I think you would still need some SQL, you could use annotate to add the field with the value of the calculation and then add .order_by() to the queryset


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9fbf9c99-c914-437b-aae2-a366a0f8eaec%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages