Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I order by a formula #475

Closed
draklowell opened this issue Jul 31, 2020 · 3 comments · Fixed by #522
Closed

Can I order by a formula #475

draklowell opened this issue Jul 31, 2020 · 3 comments · Fixed by #522
Labels
enhancement New feature or request

Comments

@draklowell
Copy link

I want to order by formula between two columns. In sql query it look like SELECT * FROM places ORDER BY (latitude-1)+(longitude-1) in SQLite, but i need it in QuerySet. Sorry, my english is well

@harrisj
Copy link

harrisj commented Oct 11, 2020

To echo this request, it would be useful to be able to provide a raw string for the order declaration that doesn't lookup fields. This would allow me to do ordering based on summing different fields or doing an order by RANDOM(), both of which require me to construct a raw SQL query

@long2ice long2ice added the enhancement New feature or request label Oct 11, 2020
@long2ice
Copy link
Member

Yes, this is a good feature.

@long2ice
Copy link
Member

Current support order by function already, like

await model.annotate(a=Length('field')).order_by('-a')

but not for expression, like

await model.annotate(idp=F("id") + 1).order_by("-idp").values_list("id", "idp")

And will support that in next release.

long2ice added a commit that referenced this issue Oct 11, 2020
Support `F` expression in `annotate`. (#475)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants