diff options
| author | Simon Charette <charette.s@gmail.com> | 2014-04-26 03:34:20 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2014-04-30 14:23:19 -0400 |
| commit | 24ec9538b7ca400f68ba08fab380445ca95d7c02 (patch) | |
| tree | 6aedd8c704ceb9643585ab2b7bc6254d9a12f6bc /docs | |
| parent | a5f6cbce07b5f3ab48d931e3fd1883c757fb9b45 (diff) | |
Fixed #19195 -- Allow explicit ordering by a relation `_id` field.
Thanks to chrisedgemon for the report and shaib, akaariai and
timgraham for the review.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 27 | ||||
| -rw-r--r-- | docs/releases/1.7.txt | 3 |
2 files changed, 30 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 84b0441026..d47f8d3a7f 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -294,6 +294,18 @@ primary key if there is no :attr:`Meta.ordering ...since the ``Blog`` model has no default ordering specified. +.. versionadded:: 1.7 + + Note that it is also possible to order a queryset by a related field, + without incurring the cost of a JOIN, by referring to the ``_id`` of the + related field:: + + # No Join + Entry.objects.order_by('blog_id') + + # Join + Entry.objects.order_by('blog__id') + Be cautious when ordering by fields in related models if you are also using :meth:`distinct()`. See the note in :meth:`distinct` for an explanation of how related model ordering can change the expected results. @@ -435,6 +447,21 @@ Examples (those after the first will only work on PostgreSQL):: >>> Entry.objects.order_by('author', 'pub_date').distinct('author') [...] +.. note:: + Keep in mind that :meth:`order_by` uses any default related model ordering + that has been defined. You might have to explicitly order by the relation + ``_id`` or referenced field to make sure the ``DISTINCT ON`` expressions + match those at the beginning of the ``ORDER BY`` clause. For example, if + the ``Blog`` model defined an :attr:`~django.db.models.Options.ordering` by + ``name``:: + + Entry.objects.order_by('blog').distinct('blog') + + ...wouldn't work because the query would be ordered by ``blog__name`` thus + mismatching the ``DISTINCT ON`` expression. You'd have to explicitly order + by the relation `_id` field (``blog_id`` in this case) or the referenced + one (``blog__pk``) to make sure both expressions match. + values ~~~~~~ diff --git a/docs/releases/1.7.txt b/docs/releases/1.7.txt index fde451d2c8..3a2635b9af 100644 --- a/docs/releases/1.7.txt +++ b/docs/releases/1.7.txt @@ -702,6 +702,9 @@ Models Previously model field validation didn't prevent values out of their associated column data type range from being saved resulting in an integrity error. +* It is now possible to explicitly :meth:`~django.db.models.query.QuerySet.order_by` + a relation ``_id`` field by using its attribute name. + Signals ^^^^^^^ |
