2015-12-19 14:52:17 +01:00
|
|
|
title: order_by
|
|
|
|
---
|
|
|
|
summary: Changes the ordering of the query.
|
|
|
|
---
|
|
|
|
type: method
|
|
|
|
---
|
|
|
|
signature: *fields
|
|
|
|
---
|
|
|
|
body:
|
|
|
|
|
|
|
|
This is a handy way to change the order of the items returned. The default
|
2016-02-11 01:13:37 +01:00
|
|
|
order is defined in the model config but can be overridden this way. The
|
2015-12-19 14:52:17 +01:00
|
|
|
method accepts an arbitrary number of strings, each of which refers to the
|
|
|
|
name of a field. If a string is prefixed with a minus sign (`-`) then the
|
|
|
|
order is reversed.
|
|
|
|
|
|
|
|
If two records have the same value for a field, then the ordering is defined on
|
|
|
|
the next argument given. So if you order by (`'year', 'name'`) it will first
|
|
|
|
order by year and within a year it will order by name.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
```html+jinja
|
|
|
|
<ul>
|
|
|
|
{% for project in this.children.order_by('-year', 'name') %}
|
2016-07-22 17:47:09 +02:00
|
|
|
<li>{{ project.year }}: {{ project.name }}</li>
|
2015-12-19 14:52:17 +01:00
|
|
|
{% endif %}
|
|
|
|
</ul>
|
|
|
|
```
|