Ticket #8634: t8634.diff

File t8634.diff, 2.6 KB (added by Ramiro Morales, 16 years ago)
  • docs/ref/models/querysets.txt

    diff -r ca5c0310d11b docs/ref/models/querysets.txt
    a b Note: ``order_by('?')`` queries may be e  
    145145Note: ``order_by('?')`` queries may be expensive and slow, depending on the
    146146database backend you're using.
    147147
    148 To order by a field in a different table, add the other table's name and a dot,
    149 like so::
     148To order by a field in a different model, use the same syntax as when you are
     149querying across model relations. That is, the name of the field, followed by a
     150double underscore (``__``), followed by the name of the field in the new model,
     151and so on for as many models as you want to join. For example::
    150152
    151     Entry.objects.order_by('blogs_blog.name', 'headline')
     153    Entry.objects.order_by('blog__name', 'headline')
     154
     155If you try to order by a field that is a relation to another model, Django will
     156use the default ordering on the related model (or order by the related model's
     157primary key if there is no ``Meta.ordering`` specified. For example::
     158
     159    Entry.objects.order_by('blog')
     160
     161...is identical to::
     162
     163    Entry.objects.order_by('blog__id')
     164
     165...since the ``Blog`` model has no default ordering specified.
     166
     167Be cautious when ordering by fields in related models if you are also using
     168``distinct()``. See the note in the `distinct()`_ section for an explanation
     169of how related model ordering can change the expected results.
     170
     171It is permissible to specify a multi-valued field to order the results by (for
     172example, a ``ManyToMany`` field). Normally this won't be a sensible thing to
     173do and it's really an advanced usage feature. However, if you know that your
     174queryset's filtering or available data implies that there will only be one
     175ordering piece of data for each of the main items you are selecting, the
     176ordering may well be exactly what you want to do. Use ordering on multi-valued
     177fields with care and make sure the results are what you expect.
     178
     179**New in Django development version:** If you don't want any ordering to be
     180applied to a query, not even the default ordering, call ``order_by()`` with no
     181parameters.
     182
     183**New in Django development version:** The syntax for ordering across related
     184models has changed. See the `Django 0.96 documentation`_ for the old behaviour.
     185
     186.. _Django 0.96 documentation: http://www.djangoproject.com/documentation/0.96/model-api/#floatfield
    152187
    153188There's no way to specify whether ordering should be case sensitive. With
    154189respect to case-sensitivity, Django will order results however your database
Back to Top