From 0f2c4bf2256aa645d3d954e3cd99921171b829ca Mon Sep 17 00:00:00 2001
From: Derek Willis <dwillis@gmail.com>
Date: Sun, 28 Feb 2010 21:15:37 -0500
Subject: [PATCH] added metadata targets to querysets ref
---
docs/ref/models/querysets.txt | 72 +++++++++++++---------------------------
1 files changed, 24 insertions(+), 48 deletions(-)
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt
index b334240..96462b3 100644
a
|
b
|
Django provides a range of ``QuerySet`` refinement methods that modify either
|
135 | 135 | the types of results returned by the ``QuerySet`` or the way its SQL query is |
136 | 136 | executed. |
137 | 137 | |
138 | | ``filter(**kwargs)`` |
139 | | ~~~~~~~~~~~~~~~~~~~~ |
| 138 | .. method:: filter(**kwargs) |
140 | 139 | |
141 | 140 | Returns a new ``QuerySet`` containing objects that match the given lookup |
142 | 141 | parameters. |
… |
… |
The lookup parameters (``**kwargs``) should be in the format described in
|
145 | 144 | `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the |
146 | 145 | underlying SQL statement. |
147 | 146 | |
148 | | ``exclude(**kwargs)`` |
149 | | ~~~~~~~~~~~~~~~~~~~~~ |
| 147 | .. method:: exclude(**kwargs) |
150 | 148 | |
151 | 149 | Returns a new ``QuerySet`` containing objects that do *not* match the given |
152 | 150 | lookup parameters. |
… |
… |
In SQL terms, that evaluates to::
|
178 | 176 | |
179 | 177 | Note the second example is more restrictive. |
180 | 178 | |
181 | | ``annotate(*args, **kwargs)`` |
182 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 | .. method:: annotate(*args, **kwargs) |
183 | 180 | |
184 | 181 | .. versionadded:: 1.1 |
185 | 182 | |
… |
… |
control the name of the annotation::
|
220 | 217 | For an in-depth discussion of aggregation, see :ref:`the topic guide on |
221 | 218 | Aggregation <topics-db-aggregation>`. |
222 | 219 | |
223 | | ``order_by(*fields)`` |
224 | | ~~~~~~~~~~~~~~~~~~~~~ |
| 220 | .. method:: order_by(*fields) |
225 | 221 | |
226 | 222 | By default, results returned by a ``QuerySet`` are ordered by the ordering |
227 | 223 | tuple given by the ``ordering`` option in the model's ``Meta``. You can |
… |
… |
undefined afterward).
|
324 | 320 | |
325 | 321 | .. _queryset-distinct: |
326 | 322 | |
327 | | ``distinct()`` |
328 | | ~~~~~~~~~~~~~~ |
| 323 | .. method:: distinct() |
329 | 324 | |
330 | 325 | Returns a new ``QuerySet`` that uses ``SELECT DISTINCT`` in its SQL query. This |
331 | 326 | eliminates duplicate rows from the query results. |
… |
… |
query spans multiple tables, it's possible to get duplicate results when a
|
358 | 353 | |
359 | 354 | .. _queryset-values: |
360 | 355 | |
361 | | ``values(*fields)`` |
362 | | ~~~~~~~~~~~~~~~~~~~ |
| 356 | .. method:: values(*fields) |
363 | 357 | |
364 | 358 | Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of |
365 | 359 | dictionaries instead of model-instance objects. |
… |
… |
followed (optionally) by any output-affecting methods (such as ``values()``),
|
441 | 435 | but it doesn't really matter. This is your chance to really flaunt your |
442 | 436 | individualism. |
443 | 437 | |
444 | | ``values_list(*fields)`` |
445 | | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 438 | .. method:: values_list(*fields) |
446 | 439 | |
447 | 440 | .. versionadded:: 1.0 |
448 | 441 | |
… |
… |
It is an error to pass in ``flat`` when there is more than one field.
|
469 | 462 | If you don't pass any values to ``values_list()``, it will return all the |
470 | 463 | fields in the model, in the order they were declared. |
471 | 464 | |
472 | | ``dates(field, kind, order='ASC')`` |
473 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 465 | .. method:: dates(field, kind, order='ASC') |
474 | 466 | |
475 | 467 | Returns a ``DateQuerySet`` -- a ``QuerySet`` that evaluates to a list of |
476 | 468 | ``datetime.datetime`` objects representing all available dates of a particular |
… |
… |
Examples::
|
503 | 495 | >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day') |
504 | 496 | [datetime.datetime(2005, 3, 20)] |
505 | 497 | |
506 | | ``none()`` |
507 | | ~~~~~~~~~~ |
| 498 | .. method:: none() |
508 | 499 | |
509 | 500 | .. versionadded:: 1.0 |
510 | 501 | |
… |
… |
Examples::
|
518 | 509 | >>> Entry.objects.none() |
519 | 510 | [] |
520 | 511 | |
521 | | ``all()`` |
522 | | ~~~~~~~~~~ |
| 512 | .. method:: all() |
523 | 513 | |
524 | 514 | .. versionadded:: 1.0 |
525 | 515 | |
… |
… |
definitely have a ``QuerySet`` to work with.
|
531 | 521 | |
532 | 522 | .. _select-related: |
533 | 523 | |
534 | | ``select_related()`` |
535 | | ~~~~~~~~~~~~~~~~~~~~ |
| 524 | .. method:: select_related() |
536 | 525 | |
537 | 526 | Returns a ``QuerySet`` that will automatically "follow" foreign-key |
538 | 527 | relationships, selecting that additional related-object data when it executes |
… |
… |
are performing a depth-based ``select_related``.
|
652 | 641 | |
653 | 642 | .. _queryset-extra: |
654 | 643 | |
655 | | ``extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)`` |
656 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 644 | .. method:: extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None) |
657 | 645 | |
658 | 646 | Sometimes, the Django query syntax by itself can't easily express a complex |
659 | 647 | ``WHERE`` clause. For these edge cases, Django provides the ``extra()`` |
… |
… |
of the arguments is required, but you should use at least one of them.
|
814 | 802 | |
815 | 803 | .. _queryset-defer: |
816 | 804 | |
817 | | ``defer(*fields)`` |
818 | | ~~~~~~~~~~~~~~~~~~ |
| 805 | .. method:: defer(*fields) |
819 | 806 | |
820 | 807 | .. versionadded:: 1.1 |
821 | 808 | |
… |
… |
eventually).
|
872 | 859 | bother using ``defer()``; leave it until your query construction has |
873 | 860 | settled down and you understand where the hot-points are. |
874 | 861 | |
875 | | ``only(*fields)`` |
876 | | ~~~~~~~~~~~~~~~~~~ |
| 862 | .. method:: only(*fields) |
877 | 863 | |
878 | 864 | .. versionadded:: 1.1 |
879 | 865 | |
… |
… |
logically::
|
908 | 894 | # existing set of fields). |
909 | 895 | Entry.objects.defer("body").only("headline", "body") |
910 | 896 | |
911 | | ``using(alias)`` |
912 | | ~~~~~~~~~~~~~~~~~~ |
| 897 | .. method:: using(alias) |
913 | 898 | |
914 | 899 | .. versionadded:: 1.2 |
915 | 900 | |
… |
… |
they query the database each time they're called.
|
938 | 923 | |
939 | 924 | .. _get-kwargs: |
940 | 925 | |
941 | | ``get(**kwargs)`` |
942 | | ~~~~~~~~~~~~~~~~~ |
| 926 | .. method:: get(**kwargs) |
943 | 927 | |
944 | 928 | Returns the object matching the given lookup parameters, which should be in |
945 | 929 | the format described in `Field lookups`_. |
… |
… |
The ``DoesNotExist`` exception inherits from
|
965 | 949 | except ObjectDoesNotExist: |
966 | 950 | print "Either the entry or blog doesn't exist." |
967 | 951 | |
968 | | ``create(**kwargs)`` |
969 | | ~~~~~~~~~~~~~~~~~~~~ |
| 952 | .. method:: create(**kwargs) |
970 | 953 | |
971 | 954 | A convenience method for creating an object and saving it all in one step. Thus:: |
972 | 955 | |
… |
… |
database, a call to ``create()`` will fail with an ``IntegrityError`` since
|
987 | 970 | primary keys must be unique. So remember to be prepared to handle the |
988 | 971 | exception if you are using manual primary keys. |
989 | 972 | |
990 | | ``get_or_create(**kwargs)`` |
991 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 973 | .. method:: get_or_create(**kwargs) |
992 | 974 | |
993 | 975 | A convenience method for looking up an object with the given kwargs, creating |
994 | 976 | one if necessary. |
… |
… |
has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec.
|
1055 | 1037 | |
1056 | 1038 | .. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 |
1057 | 1039 | |
1058 | | ``count()`` |
1059 | | ~~~~~~~~~~~ |
| 1040 | .. method:: count() |
1060 | 1041 | |
1061 | 1042 | Returns an integer representing the number of objects in the database matching |
1062 | 1043 | the ``QuerySet``. ``count()`` never raises exceptions. |
… |
… |
Depending on which database you're using (e.g. PostgreSQL vs. MySQL),
|
1079 | 1060 | is an underlying implementation quirk that shouldn't pose any real-world |
1080 | 1061 | problems. |
1081 | 1062 | |
1082 | | ``in_bulk(id_list)`` |
1083 | | ~~~~~~~~~~~~~~~~~~~~ |
| 1063 | .. method:: in_bulk(id_list) |
1084 | 1064 | |
1085 | 1065 | Takes a list of primary-key values and returns a dictionary mapping each |
1086 | 1066 | primary-key value to an instance of the object with the given ID. |
… |
… |
If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.
|
1098 | 1078 | |
1099 | 1079 | .. _queryset-iterator: |
1100 | 1080 | |
1101 | | ``iterator()`` |
1102 | | ~~~~~~~~~~~~~~ |
| 1081 | .. method:: iterator() |
1103 | 1082 | |
1104 | 1083 | Evaluates the ``QuerySet`` (by performing the query) and returns an |
1105 | 1084 | `iterator`_ over the results. A ``QuerySet`` typically caches its |
… |
… |
been evaluated will force it to evaluate again, repeating the query.
|
1114 | 1093 | |
1115 | 1094 | .. _iterator: http://www.python.org/dev/peps/pep-0234/ |
1116 | 1095 | |
1117 | | ``latest(field_name=None)`` |
1118 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1096 | .. method:: latest(field_name=None) |
1119 | 1097 | |
1120 | 1098 | Returns the latest object in the table, by date, using the ``field_name`` |
1121 | 1099 | provided as the date field. |
… |
… |
exist with the given parameters.
|
1134 | 1112 | |
1135 | 1113 | Note ``latest()`` exists purely for convenience and readability. |
1136 | 1114 | |
1137 | | ``aggregate(*args, **kwargs)`` |
1138 | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1115 | .. method:: aggregate(*args, **kwargs) |
1139 | 1116 | |
1140 | 1117 | .. versionadded:: 1.1 |
1141 | 1118 | |
… |
… |
control the name of the aggregation value that is returned::
|
1166 | 1143 | For an in-depth discussion of aggregation, see :ref:`the topic guide on |
1167 | 1144 | Aggregation <topics-db-aggregation>`. |
1168 | 1145 | |
1169 | | ``exists()`` |
1170 | | ~~~~~~~~~~~~ |
| 1146 | .. method:: exists() |
1171 | 1147 | |
1172 | 1148 | .. versionadded:: 1.2 |
1173 | 1149 | |