| 951 | | |
|---|
| 952 | | TODO: Left off here |
|---|
| 953 | | |
|---|
| 954 | | =========== ============================================================== |
|---|
| 955 | | Type Description |
|---|
| 956 | | =========== ============================================================== |
|---|
| 957 | | month For date/datetime fields, exact month match. |
|---|
| 958 | | day For date/datetime fields, exact day match. |
|---|
| 959 | | isnull True/False; does is IF NULL/IF NOT NULL lookup: |
|---|
| 960 | | ``Poll.objects.filter(expire_date__isnull=True)``. |
|---|
| 961 | | =========== ============================================================== |
|---|
| | 956 | month |
|---|
| | 957 | ~~~~~ |
|---|
| | 958 | |
|---|
| | 959 | For date/datetime fields, exact month match. Takes an integer 1 (January) |
|---|
| | 960 | through 12 (December). |
|---|
| | 961 | |
|---|
| | 962 | Example:: |
|---|
| | 963 | |
|---|
| | 964 | Entry.objects.filter(pub_date__month=12) |
|---|
| | 965 | |
|---|
| | 966 | SQL equivalent:: |
|---|
| | 967 | |
|---|
| | 968 | SELECT ... WHERE EXTRACT('month' FROM pub_date) = '12'; |
|---|
| | 969 | |
|---|
| | 970 | (The exact SQL syntax varies for each database engine.) |
|---|
| | 971 | |
|---|
| | 972 | day |
|---|
| | 973 | ~~~ |
|---|
| | 974 | |
|---|
| | 975 | For date/datetime fields, exact day match. |
|---|
| | 976 | |
|---|
| | 977 | Example:: |
|---|
| | 978 | |
|---|
| | 979 | Entry.objects.filter(pub_date__day=3) |
|---|
| | 980 | |
|---|
| | 981 | SQL equivalent:: |
|---|
| | 982 | |
|---|
| | 983 | SELECT ... WHERE EXTRACT('day' FROM pub_date) = '3'; |
|---|
| | 984 | |
|---|
| | 985 | (The exact SQL syntax varies for each database engine.) |
|---|
| | 986 | |
|---|
| | 987 | Note this will match any record with a pub_date on the third day of the month, |
|---|
| | 988 | such as January 3, July 3, etc. |
|---|
| | 989 | |
|---|
| | 990 | isnull |
|---|
| | 991 | ~~~~~~ |
|---|
| | 992 | |
|---|
| | 993 | ``NULL`` or ``IS NOT NULL`` match. Takes either ``True`` or ``False``, which |
|---|
| | 994 | correspond to ``IS NULL`` and ``IS NOT NULL``, respectively. |
|---|
| | 995 | |
|---|
| | 996 | Example:: |
|---|
| | 997 | |
|---|
| | 998 | Entry.objects.filter(pub_date__isnull=True) |
|---|
| | 999 | |
|---|
| | 1000 | SQL equivalent:: |
|---|
| | 1001 | |
|---|
| | 1002 | SELECT ... WHERE pub_date IS NULL; |
|---|
| | 1003 | |
|---|
| | 1004 | Default lookups are exact |
|---|
| | 1005 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 1006 | |
|---|
| | 1007 | If you don't provide a lookup type -- that is, if your keyword argument doesn't |
|---|
| | 1008 | contain a double underscore -- the lookup type is assumed to be ``exact``. |
|---|
| | 1009 | |
|---|
| | 1010 | For example, the following two statements are equivalent:: |
|---|
| | 1011 | |
|---|
| | 1012 | Blog.objects.get(id=14) |
|---|
| | 1013 | Blog.objects.get(id__exact=14) |
|---|
| | 1014 | |
|---|
| | 1015 | This is for convenience, because ``exact`` lookups are the common case. |
|---|
| | 1016 | |
|---|
| | 1017 | The pk lookup shortcut |
|---|
| | 1018 | ~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 1019 | |
|---|
| | 1020 | For convenience, Django provides a ``pk`` lookup type, which stands for |
|---|
| | 1021 | "primary_key". This is shorthand for "an exact lookup on the primary-key." |
|---|
| | 1022 | |
|---|
| | 1023 | In the example ``Blog`` model, the primary key is the ``id`` field, so these |
|---|
| | 1024 | two statements are equivalent:: |
|---|
| | 1025 | |
|---|
| | 1026 | Blog.objects.get(id__exact=14) |
|---|
| | 1027 | Blog.objects.get(pk=14) |
|---|
| | 1028 | |
|---|
| | 1029 | ``pk`` lookups also work across joins. For example, these two statements are |
|---|
| | 1030 | equivalent:: |
|---|
| | 1031 | |
|---|
| | 1032 | Entry.objects.filter(blog__id__exact=3) |
|---|
| | 1033 | Entry.objects.filter(blog__pk=3) |
|---|
| 966 | | As you may expect, |
|---|
| 967 | | |
|---|
| 968 | | |
|---|
| 969 | | |
|---|
| 970 | | |
|---|
| 971 | | |
|---|
| 972 | | |
|---|
| 973 | | If no lookup type is provided, a type of ``exact`` is assumed. The following |
|---|
| 974 | | two statements are equivalent:: |
|---|
| 975 | | |
|---|
| 976 | | Poll.objects.get(id=14) |
|---|
| 977 | | Poll.objects.get(id__exact=14) |
|---|
| 978 | | |
|---|
| 979 | | Multiple lookup parameters are allowed. When separated by commans, the list of |
|---|
| 980 | | lookup parameters will be "AND"ed together:: |
|---|
| 981 | | |
|---|
| 982 | | Poll.objects.filter( |
|---|
| 983 | | pub_date__year=2005, |
|---|
| 984 | | pub_date__month=1, |
|---|
| 985 | | question__startswith="Would", |
|---|
| 986 | | ) |
|---|
| 987 | | |
|---|
| 988 | | ...retrieves all polls published in January 2005 that have a question starting |
|---|
| 989 | | with "Would." |
|---|
| 990 | | |
|---|
| 991 | | For convenience, there's a ``pk`` lookup type, which translates into |
|---|
| 992 | | ``(primary_key)``. In the polls example, these two statements are |
|---|
| 993 | | equivalent:: |
|---|
| 994 | | |
|---|
| 995 | | Poll.objects.get(id__exact=3) |
|---|
| 996 | | Poll.objects.get(pk=3) |
|---|
| 997 | | |
|---|
| 998 | | ``pk`` lookups also work across joins. In the polls example, these two |
|---|
| 999 | | statements are equivalent:: |
|---|
| 1000 | | |
|---|
| 1001 | | Choice.objects.filter(poll__id=3) |
|---|
| 1002 | | Choice.objects.filter(poll__pk=3) |
|---|
| 1003 | | |
|---|
| 1004 | | If you pass an invalid keyword argument, the function will raise ``TypeError``. |
|---|
| 1005 | | |
|---|
| 1006 | | .. _`Keyword Arguments`: http://docs.python.org/tut/node6.html#SECTION006720000000000000000 |
|---|
| | 1038 | The field lookups that equate to ``LIKE`` SQL statements will automatically |
|---|
| | 1039 | escape the two special characters used in ``LIKE`` statements -- the percent |
|---|
| | 1040 | sign and the underscore. (In a ``LIKE`` statement, the percent sign signifies |
|---|
| | 1041 | a multiple-character wildcard and the underscore signifies a single-character |
|---|
| | 1042 | wildcard.) |
|---|
| | 1043 | |
|---|
| | 1044 | This means things should work intuitively, so the abstraction doesn't leak. |
|---|
| | 1045 | For example, to retrieve all the entries that contain a percent sign, just use |
|---|
| | 1046 | the percent sign as any other character:: |
|---|
| | 1047 | |
|---|
| | 1048 | Entry.objects.filter(headline__contains='%') |
|---|
| | 1049 | |
|---|
| | 1050 | Django takes care of the quoting for you; the resulting SQL will look something |
|---|
| | 1051 | like this:: |
|---|
| | 1052 | |
|---|
| | 1053 | SELECT ... WHERE headline LIKE '%\%%'; |
|---|
| | 1054 | |
|---|
| | 1055 | Same goes for underscores. Both percentage signs and underscores are handled |
|---|
| | 1056 | for you transparently. |
|---|
| 1039 | | |
|---|
| 1040 | | |
|---|
| 1041 | | Deleting objects |
|---|
| 1042 | | ================ |
|---|
| 1043 | | |
|---|
| 1044 | | |
|---|
| | 1089 | Comparing objects |
|---|
| | 1090 | ================= |
|---|
| | 1091 | |
|---|
| | 1092 | To compare two model instances, just use the standard Python comparison operator, |
|---|
| | 1093 | the double equals sign: ``==``. Behind the scenes, that compares the primary |
|---|
| | 1094 | key values of two models. |
|---|
| | 1095 | |
|---|
| | 1096 | Using the ``Entry`` example above, the following two statements are equivalent:: |
|---|
| | 1097 | |
|---|
| | 1098 | some_entry == other_entry |
|---|
| | 1099 | some_entry.id == other_entry.id |
|---|
| | 1100 | |
|---|
| | 1101 | If a model's primary key isn't called ``id``, no problem. Comparisons will |
|---|
| | 1102 | always use the primary key, whatever it's called. For example, if a model's |
|---|
| | 1103 | primary key field is called ``name``, these two statements are equivalent:: |
|---|
| | 1104 | |
|---|
| | 1105 | some_obj == other_obj |
|---|
| | 1106 | some_obj.name == other_obj.name |
|---|
| | 1107 | |
|---|
| | 1108 | |
|---|
| | 1109 | |
|---|
| | 1110 | |
|---|
| | 1111 | ======================================== |
|---|
| | 1112 | THE REST OF THIS HAS NOT YET BEEN EDITED |
|---|
| | 1113 | ======================================== |
|---|
| 1272 | | Creating new objects |
|---|
| 1273 | | ==================== |
|---|
| 1274 | | |
|---|
| 1275 | | Creating new objects (i.e. ``INSERT``) is done by creating new instances |
|---|
| 1276 | | of objects then calling save() on them:: |
|---|
| 1277 | | |
|---|
| 1278 | | >>> p = Poll(slug="eggs", |
|---|
| 1279 | | ... question="How do you like your eggs?", |
|---|
| 1280 | | ... pub_date=datetime.datetime.now(), |
|---|
| 1281 | | ... expire_date=some_future_date) |
|---|
| 1282 | | >>> p.save() |
|---|
| 1283 | | |
|---|
| 1284 | | Calling ``save()`` on an object with a primary key whose value is ``None`` |
|---|
| 1285 | | signifies to Django that the object is new and should be inserted. |
|---|
| | 1341 | Creating new related objects |
|---|
| | 1342 | ============================ |
|---|
| 1331 | | |
|---|
| 1332 | | Comparing objects |
|---|
| 1333 | | ================= |
|---|
| 1334 | | |
|---|
| 1335 | | To compare two model objects, just use the standard Python comparison operator, |
|---|
| 1336 | | the double equals sign: ``==``. Behind the scenes, that compares the primary |
|---|
| 1337 | | key values of two models. |
|---|
| 1338 | | |
|---|
| 1339 | | Using the ``Poll`` example above, the following two statements are equivalent:: |
|---|
| 1340 | | |
|---|
| 1341 | | some_poll == other_poll |
|---|
| 1342 | | some_poll.id == other_poll.id |
|---|
| 1343 | | |
|---|
| 1344 | | If a model's primary key isn't called ID, no problem. Comparisons will always |
|---|
| 1345 | | use the primary key, whatever it's called. For example, if a model's primary |
|---|
| 1346 | | key field is called ``name``, these two statements are equivalent:: |
|---|
| 1347 | | |
|---|
| 1348 | | some_obj == other_obj |
|---|
| 1349 | | some_obj.name == other_obj.name |
|---|