mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #24060 -- Added OrderBy Expressions
This commit is contained in:
parent
f48e2258a9
commit
21b858cb67
9 changed files with 310 additions and 54 deletions
|
@ -5,7 +5,7 @@ Query Expressions
|
|||
.. currentmodule:: django.db.models
|
||||
|
||||
Query expressions describe a value or a computation that can be used as part of
|
||||
a filter, an annotation, or an aggregation. There are a number of built-in
|
||||
a filter, order by, annotation, or aggregate. There are a number of built-in
|
||||
expressions (documented below) that can be used to help you write queries.
|
||||
Expressions can be combined, or in some cases nested, to form more complex
|
||||
computations.
|
||||
|
@ -58,6 +58,10 @@ Some examples
|
|||
# Aggregates can contain complex computations also
|
||||
Company.objects.annotate(num_offerings=Count(F('products') + F('services')))
|
||||
|
||||
# Expressions can also be used in order_by()
|
||||
Company.objects.order_by(Length('name').asc())
|
||||
Company.objects.order_by(Length('name').desc())
|
||||
|
||||
|
||||
Built-in Expressions
|
||||
====================
|
||||
|
@ -428,6 +432,24 @@ calling the appropriate methods on the wrapped expression.
|
|||
nested expressions. ``F()`` objects, in particular, hold a reference
|
||||
to a column.
|
||||
|
||||
.. method:: asc()
|
||||
|
||||
Returns the expression ready to be sorted in ascending order.
|
||||
|
||||
.. method:: desc()
|
||||
|
||||
Returns the expression ready to be sorted in descending order.
|
||||
|
||||
.. method:: reverse_ordering()
|
||||
|
||||
Returns ``self`` with any modifications required to reverse the sort
|
||||
order within an ``order_by`` call. As an example, an expression
|
||||
implementing ``NULLS LAST`` would change its value to be
|
||||
``NULLS FIRST``. Modifications are only required for expressions that
|
||||
implement sort order like ``OrderBy``. This method is called when
|
||||
:meth:`~django.db.models.query.QuerySet.reverse()` is called on a
|
||||
queryset.
|
||||
|
||||
Writing your own Query Expressions
|
||||
----------------------------------
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue