| 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') |
|---|
| | 148 | To order by a field in a different model, use the same syntax as when you are |
|---|
| | 149 | querying across model relations. That is, the name of the field, followed by a |
|---|
| | 150 | double underscore (``__``), followed by the name of the field in the new model, |
|---|
| | 151 | and so on for as many models as you want to join. For example:: |
|---|
| | 152 | |
|---|
| | 153 | Entry.objects.order_by('blog__name', 'headline') |
|---|
| | 154 | |
|---|
| | 155 | If you try to order by a field that is a relation to another model, Django will |
|---|
| | 156 | use the default ordering on the related model (or order by the related model's |
|---|
| | 157 | primary 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 | |
|---|
| | 167 | Be 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 |
|---|
| | 169 | of how related model ordering can change the expected results. |
|---|
| | 170 | |
|---|
| | 171 | It is permissible to specify a multi-valued field to order the results by (for |
|---|
| | 172 | example, a ``ManyToMany`` field). Normally this won't be a sensible thing to |
|---|
| | 173 | do and it's really an advanced usage feature. However, if you know that your |
|---|
| | 174 | queryset's filtering or available data implies that there will only be one |
|---|
| | 175 | ordering piece of data for each of the main items you are selecting, the |
|---|
| | 176 | ordering may well be exactly what you want to do. Use ordering on multi-valued |
|---|
| | 177 | fields 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 |
|---|
| | 180 | applied to a query, not even the default ordering, call ``order_by()`` with no |
|---|
| | 181 | parameters. |
|---|
| | 182 | |
|---|
| | 183 | **New in Django development version:** The syntax for ordering across related |
|---|
| | 184 | models 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 |
|---|
| 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()``. |
|---|
| | 209 | don't introduce the possibility of duplicate result rows. However, if your |
|---|
| | 210 | query 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 | |
|---|
| | 266 | A 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 | |
|---|
| | 313 | |
|---|
| | 314 | ``values_list(*fields)`` |
|---|
| | 315 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 316 | |
|---|
| | 317 | **New in Django development version** |
|---|
| | 318 | |
|---|
| | 319 | This is similar to ``values()`` except that instead of returning a list of |
|---|
| | 320 | dictionaries, it returns a list of tuples. Each tuple contains the value from |
|---|
| | 321 | the respective field passed into the ``values_list()`` call -- so the first |
|---|
| | 322 | item is the first field, etc. For example:: |
|---|
| | 323 | |
|---|
| | 324 | >>> Entry.objects.values_list('id', 'headline') |
|---|
| | 325 | [(1, u'First entry'), ...] |
|---|
| | 326 | |
|---|
| | 327 | If you only pass in a single field, you can also pass in the ``flat`` |
|---|
| | 328 | parameter. If ``True``, this will mean the returned results are single values, |
|---|
| | 329 | rather 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 | |
|---|
| | 337 | It is an error to pass in ``flat`` when there is more than one field. |
|---|
| | 338 | |
|---|
| | 339 | If you don't pass any values to ``values_list()``, it will return all the |
|---|
| | 340 | fields in the model, in the order they were declared. |
|---|