Django

Code

Changeset 8694

Show
Ignore:
Timestamp:
08/29/08 00:04:26 (3 months ago)
Author:
mtredinnick
Message:

Revived a bunch of missing documentation that got lost in the docs-refactor.
This describes values_list(), the new cross-model order_by() syntax and the
effects of distinct(), values() and order_by() on each other.

Fixed #8634.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/ref/models/querysets.txt

    r8670 r8694  
    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:: 
    150  
    151     Entry.objects.order_by('blogs_blog.name', 'headline') 
     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:: 
     152 
     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 
     
    172207By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this 
    173208is rarely a problem, because simple queries such as ``Blog.objects.all()`` 
    174 don't introduce the possibility of duplicate result rows. 
    175  
    176 However, if your query spans multiple tables, it's possible to get duplicate 
    177 results when a ``QuerySet`` is evaluated. That's when you'd use ``distinct()``. 
     209don't introduce the possibility of duplicate result rows. However, if your 
     210query spans multiple tables, it's possible to get duplicate results when a 
     211``QuerySet`` is evaluated. That's when you'd use ``distinct()``. 
     212 
     213.. note:: 
     214    Any fields used in an `order_by(*fields)`_ call are included in the SQL 
     215    ``SELECT`` columns. This can sometimes lead to unexpected results when 
     216    used in conjunction with ``distinct()``. If you order by fields from a 
     217    related model, those fields will be added to the selected columns and they 
     218    may make otherwise duplicate rows appear to be distinct. Since the extra 
     219    columns don't appear in the returned results (they are only there to 
     220    support ordering), it sometimes looks like non-distinct results are being 
     221    returned. 
     222 
     223    Similarly, if you use a ``values()`` query to restrict the columns 
     224    selected, the columns used in any ``order_by()`` (or default model 
     225    ordering) will still be involved and may affect uniqueness of the results. 
     226 
     227    The moral here is that if you are using ``distinct()`` be careful about 
     228    ordering by related models. Similarly, when using ``distinct()`` and 
     229    ``values()`` together, be careful when ordering by fields not in the 
     230    ``values()`` call. 
     231 
    178232 
    179233``values(*fields)`` 
     
    210264    [{'id': 1, 'name': 'Beatles Blog'}] 
    211265 
     266A couple of subtleties that are worth mentioning: 
     267 
     268    * The ``values()`` method does not return anything for 
     269      :class:`~django.db.models.ManyToManyField` attributes and will raise an 
     270      error if you try to pass in this type of field to it. 
     271    * If you have a field called ``foo`` that is a 
     272      :class:`~django.db.models.ForeignKey`, the default ``values()`` call 
     273      will return a dictionary key called ``foo_id``, since this is the name 
     274      of the hidden model attribute that stores the actual value (the ``foo`` 
     275      attribute refers to the related model). When you are calling 
     276      ``values()`` and passing in field names, you can pass in either ``foo`` 
     277      or ``foo_id`` and you will get back the same thing (the dictionary key 
     278      will match the field name you passed in). 
     279 
     280      For example:: 
     281 
     282        >>> Entry.objects.values() 
     283        [{'blog_id: 1, 'headline': u'First Entry', ...}, ...] 
     284 
     285        >>> Entry.objects.values('blog') 
     286        [{'blog': 1}, ...] 
     287 
     288        >>> Entry.objects.values('blog_id') 
     289        [{'blog_id': 1}, ...] 
     290    * When using ``values()`` together with ``distinct()``, be aware that 
     291      ordering can affect the results. See the note in the `distinct()`_ 
     292      section, above, for details. 
     293 
     294**New in Django development version:** Previously, it was not possible to pass 
     295``blog_id`` to ``values()`` in the above example, only ``blog``. 
     296 
    212297A ``ValuesQuerySet`` is useful when you know you're only going to need values 
    213298from a small number of the available fields and you won't need the 
     
    226311but it doesn't really matter. This is your chance to really flaunt your 
    227312individualism. 
     313 
     314``values_list(*fields)`` 
     315~~~~~~~~~~~~~~~~~~~~~~~~ 
     316 
     317**New in Django development version** 
     318 
     319This is similar to ``values()`` except that instead of returning a list of 
     320dictionaries, it returns a list of tuples. Each tuple contains the value from 
     321the respective field passed into the ``values_list()`` call -- so the first 
     322item is the first field, etc. For example:: 
     323 
     324    >>> Entry.objects.values_list('id', 'headline') 
     325    [(1, u'First entry'), ...] 
     326 
     327If you only pass in a single field, you can also pass in the ``flat`` 
     328parameter. If ``True``, this will mean the returned results are single values, 
     329rather than one-tuples. An example should make the difference clearer:: 
     330 
     331    >>> Entry.objects.values_list('id').order_by('id') 
     332    [(1,), (2,), (3,), ...] 
     333 
     334    >>> Entry.objects.values_list('id', flat=True).order_by('id') 
     335    [1, 2, 3, ...] 
     336 
     337It is an error to pass in ``flat`` when there is more than one field. 
     338 
     339If you don't pass any values to ``values_list()``, it will return all the 
     340fields in the model, in the order they were declared. 
    228341 
    229342``dates(field, kind, order='ASC')``