Doing maths on fields in a query

129 views
Skip to first unread message

Thorsten

unread,
Aug 15, 2011, 10:27:44 AM8/15/11
to Django users
Hello,

I am wondering if it is possible to do math on fields inside a query
with using the django syntax for example like

SELECT (buyout / quantity) as buyout_per_item

Currently I am using raw queries, because i couldn't find anything
about such cases.

Greetings,

Daniel Roseman

unread,
Aug 15, 2011, 11:00:34 AM8/15/11
to django...@googlegroups.com
You can use the `extra()` model manager method to include extra calculated fields in your queries.

I would say though that if you're just doing simple calculations, you're not really gaining anything by doing them in the database. It's just as easy to create a model method that returns the calculated value, which you can call as necessary.
--
DR.

Thorsten

unread,
Aug 15, 2011, 11:22:52 AM8/15/11
to django...@googlegroups.com
Thank you, it is working, seems I missed that somehow while searching in the documentation.

P.S. It is not that simple, I just prefer to keep the example as simple as possible, it also includes doing min,avg,max calculations over thousands of entries etc.

Reply all
Reply to author
Forward
0 new messages