fields.txt 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. ================================
  2. PostgreSQL specific model fields
  3. ================================
  4. All of these fields are available from the ``django.contrib.postgres.fields``
  5. module.
  6. .. currentmodule:: django.contrib.postgres.fields
  7. Indexing these fields
  8. =====================
  9. :class:`~django.db.models.Index` and :attr:`.Field.db_index` both create a
  10. B-tree index, which isn't particularly helpful when querying complex data types.
  11. Indexes such as :class:`~django.contrib.postgres.indexes.GinIndex` and
  12. :class:`~django.contrib.postgres.indexes.GistIndex` are better suited, though
  13. the index choice is dependent on the queries that you're using. Generally, GiST
  14. may be a good choice for the :ref:`range fields <range-fields>` and
  15. :class:`HStoreField`, and GIN may be helpful for :class:`ArrayField` and
  16. :class:`JSONField`.
  17. ``ArrayField``
  18. ==============
  19. .. class:: ArrayField(base_field, size=None, **options)
  20. A field for storing lists of data. Most field types can be used, and you
  21. pass another field instance as the :attr:`base_field
  22. <ArrayField.base_field>`. You may also specify a :attr:`size
  23. <ArrayField.size>`. ``ArrayField`` can be nested to store multi-dimensional
  24. arrays.
  25. If you give the field a :attr:`~django.db.models.Field.default`, ensure
  26. it's a callable such as ``list`` (for an empty default) or a callable that
  27. returns a list (such as a function). Incorrectly using ``default=[]``
  28. creates a mutable default that is shared between all instances of
  29. ``ArrayField``.
  30. .. attribute:: base_field
  31. This is a required argument.
  32. Specifies the underlying data type and behavior for the array. It
  33. should be an instance of a subclass of
  34. :class:`~django.db.models.Field`. For example, it could be an
  35. :class:`~django.db.models.IntegerField` or a
  36. :class:`~django.db.models.CharField`. Most field types are permitted,
  37. with the exception of those handling relational data
  38. (:class:`~django.db.models.ForeignKey`,
  39. :class:`~django.db.models.OneToOneField` and
  40. :class:`~django.db.models.ManyToManyField`).
  41. It is possible to nest array fields - you can specify an instance of
  42. ``ArrayField`` as the ``base_field``. For example::
  43. from django.contrib.postgres.fields import ArrayField
  44. from django.db import models
  45. class ChessBoard(models.Model):
  46. board = ArrayField(
  47. ArrayField(
  48. models.CharField(max_length=10, blank=True),
  49. size=8,
  50. ),
  51. size=8,
  52. )
  53. Transformation of values between the database and the model, validation
  54. of data and configuration, and serialization are all delegated to the
  55. underlying base field.
  56. .. attribute:: size
  57. This is an optional argument.
  58. If passed, the array will have a maximum size as specified. This will
  59. be passed to the database, although PostgreSQL at present does not
  60. enforce the restriction.
  61. .. note::
  62. When nesting ``ArrayField``, whether you use the ``size`` parameter or not,
  63. PostgreSQL requires that the arrays are rectangular::
  64. from django.contrib.postgres.fields import ArrayField
  65. from django.db import models
  66. class Board(models.Model):
  67. pieces = ArrayField(ArrayField(models.IntegerField()))
  68. # Valid
  69. Board(pieces=[
  70. [2, 3],
  71. [2, 1],
  72. ])
  73. # Not valid
  74. Board(pieces=[
  75. [2, 3],
  76. [2],
  77. ])
  78. If irregular shapes are required, then the underlying field should be made
  79. nullable and the values padded with ``None``.
  80. Querying ``ArrayField``
  81. -----------------------
  82. There are a number of custom lookups and transforms for :class:`ArrayField`.
  83. We will use the following example model::
  84. from django.contrib.postgres.fields import ArrayField
  85. from django.db import models
  86. class Post(models.Model):
  87. name = models.CharField(max_length=200)
  88. tags = ArrayField(models.CharField(max_length=200), blank=True)
  89. def __str__(self):
  90. return self.name
  91. .. fieldlookup:: arrayfield.contains
  92. ``contains``
  93. ~~~~~~~~~~~~
  94. The :lookup:`contains` lookup is overridden on :class:`ArrayField`. The
  95. returned objects will be those where the values passed are a subset of the
  96. data. It uses the SQL operator ``@>``. For example::
  97. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  98. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  99. >>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
  100. >>> Post.objects.filter(tags__contains=['thoughts'])
  101. <QuerySet [<Post: First post>, <Post: Second post>]>
  102. >>> Post.objects.filter(tags__contains=['django'])
  103. <QuerySet [<Post: First post>, <Post: Third post>]>
  104. >>> Post.objects.filter(tags__contains=['django', 'thoughts'])
  105. <QuerySet [<Post: First post>]>
  106. .. fieldlookup:: arrayfield.contained_by
  107. ``contained_by``
  108. ~~~~~~~~~~~~~~~~
  109. This is the inverse of the :lookup:`contains <arrayfield.contains>` lookup -
  110. the objects returned will be those where the data is a subset of the values
  111. passed. It uses the SQL operator ``<@``. For example::
  112. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  113. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  114. >>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
  115. >>> Post.objects.filter(tags__contained_by=['thoughts', 'django'])
  116. <QuerySet [<Post: First post>, <Post: Second post>]>
  117. >>> Post.objects.filter(tags__contained_by=['thoughts', 'django', 'tutorial'])
  118. <QuerySet [<Post: First post>, <Post: Second post>, <Post: Third post>]>
  119. .. fieldlookup:: arrayfield.overlap
  120. ``overlap``
  121. ~~~~~~~~~~~
  122. Returns objects where the data shares any results with the values passed. Uses
  123. the SQL operator ``&&``. For example::
  124. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  125. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  126. >>> Post.objects.create(name='Third post', tags=['tutorial', 'django'])
  127. >>> Post.objects.filter(tags__overlap=['thoughts'])
  128. <QuerySet [<Post: First post>, <Post: Second post>]>
  129. >>> Post.objects.filter(tags__overlap=['thoughts', 'tutorial'])
  130. <QuerySet [<Post: First post>, <Post: Second post>, <Post: Third post>]>
  131. .. fieldlookup:: arrayfield.len
  132. ``len``
  133. ~~~~~~~
  134. Returns the length of the array. The lookups available afterwards are those
  135. available for :class:`~django.db.models.IntegerField`. For example::
  136. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  137. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  138. >>> Post.objects.filter(tags__len=1)
  139. <QuerySet [<Post: Second post>]>
  140. .. fieldlookup:: arrayfield.index
  141. Index transforms
  142. ~~~~~~~~~~~~~~~~
  143. Index transforms index into the array. Any non-negative integer can be used.
  144. There are no errors if it exceeds the :attr:`size <ArrayField.size>` of the
  145. array. The lookups available after the transform are those from the
  146. :attr:`base_field <ArrayField.base_field>`. For example::
  147. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  148. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  149. >>> Post.objects.filter(tags__0='thoughts')
  150. <QuerySet [<Post: First post>, <Post: Second post>]>
  151. >>> Post.objects.filter(tags__1__iexact='Django')
  152. <QuerySet [<Post: First post>]>
  153. >>> Post.objects.filter(tags__276='javascript')
  154. <QuerySet []>
  155. .. note::
  156. PostgreSQL uses 1-based indexing for array fields when writing raw SQL.
  157. However these indexes and those used in :lookup:`slices <arrayfield.slice>`
  158. use 0-based indexing to be consistent with Python.
  159. .. fieldlookup:: arrayfield.slice
  160. Slice transforms
  161. ~~~~~~~~~~~~~~~~
  162. Slice transforms take a slice of the array. Any two non-negative integers can
  163. be used, separated by a single underscore. The lookups available after the
  164. transform do not change. For example::
  165. >>> Post.objects.create(name='First post', tags=['thoughts', 'django'])
  166. >>> Post.objects.create(name='Second post', tags=['thoughts'])
  167. >>> Post.objects.create(name='Third post', tags=['django', 'python', 'thoughts'])
  168. >>> Post.objects.filter(tags__0_1=['thoughts'])
  169. <QuerySet [<Post: First post>, <Post: Second post>]>
  170. >>> Post.objects.filter(tags__0_2__contains=['thoughts'])
  171. <QuerySet [<Post: First post>, <Post: Second post>]>
  172. .. note::
  173. PostgreSQL uses 1-based indexing for array fields when writing raw SQL.
  174. However these slices and those used in :lookup:`indexes <arrayfield.index>`
  175. use 0-based indexing to be consistent with Python.
  176. .. admonition:: Multidimensional arrays with indexes and slices
  177. PostgreSQL has some rather esoteric behavior when using indexes and slices
  178. on multidimensional arrays. It will always work to use indexes to reach
  179. down to the final underlying data, but most other slices behave strangely
  180. at the database level and cannot be supported in a logical, consistent
  181. fashion by Django.
  182. ``CIText`` fields
  183. =================
  184. .. class:: CIText(**options)
  185. A mixin to create case-insensitive text fields backed by the citext_ type.
  186. Read about `the performance considerations`_ prior to using it.
  187. To use ``citext``, use the :class:`.CITextExtension` operation to
  188. :ref:`setup the citext extension <create-postgresql-extensions>` in
  189. PostgreSQL before the first ``CreateModel`` migration operation.
  190. If you're using an :class:`~django.contrib.postgres.fields.ArrayField`
  191. of ``CIText`` fields, you must add ``'django.contrib.postgres'`` in your
  192. :setting:`INSTALLED_APPS`, otherwise field values will appear as strings
  193. like ``'{thoughts,django}'``.
  194. Several fields that use the mixin are provided:
  195. .. class:: CICharField(**options)
  196. .. class:: CIEmailField(**options)
  197. .. class:: CITextField(**options)
  198. These fields subclass :class:`~django.db.models.CharField`,
  199. :class:`~django.db.models.EmailField`, and
  200. :class:`~django.db.models.TextField`, respectively.
  201. ``max_length`` won't be enforced in the database since ``citext`` behaves
  202. similar to PostgreSQL's ``text`` type.
  203. .. _citext: https://www.postgresql.org/docs/current/citext.html
  204. .. _the performance considerations: https://www.postgresql.org/docs/current/citext.html#id-1.11.7.17.7
  205. ``HStoreField``
  206. ===============
  207. .. class:: HStoreField(**options)
  208. A field for storing key-value pairs. The Python data type used is a
  209. ``dict``. Keys must be strings, and values may be either strings or nulls
  210. (``None`` in Python).
  211. To use this field, you'll need to:
  212. #. Add ``'django.contrib.postgres'`` in your :setting:`INSTALLED_APPS`.
  213. #. :ref:`Setup the hstore extension <create-postgresql-extensions>` in
  214. PostgreSQL.
  215. You'll see an error like ``can't adapt type 'dict'`` if you skip the first
  216. step, or ``type "hstore" does not exist`` if you skip the second.
  217. .. note::
  218. On occasions it may be useful to require or restrict the keys which are
  219. valid for a given field. This can be done using the
  220. :class:`~django.contrib.postgres.validators.KeysValidator`.
  221. Querying ``HStoreField``
  222. ------------------------
  223. In addition to the ability to query by key, there are a number of custom
  224. lookups available for ``HStoreField``.
  225. We will use the following example model::
  226. from django.contrib.postgres.fields import HStoreField
  227. from django.db import models
  228. class Dog(models.Model):
  229. name = models.CharField(max_length=200)
  230. data = HStoreField()
  231. def __str__(self):
  232. return self.name
  233. .. fieldlookup:: hstorefield.key
  234. Key lookups
  235. ~~~~~~~~~~~
  236. To query based on a given key, you can use that key as the lookup name::
  237. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})
  238. >>> Dog.objects.create(name='Meg', data={'breed': 'collie'})
  239. >>> Dog.objects.filter(data__breed='collie')
  240. <QuerySet [<Dog: Meg>]>
  241. You can chain other lookups after key lookups::
  242. >>> Dog.objects.filter(data__breed__contains='l')
  243. <QuerySet [<Dog: Rufus>, <Dog: Meg>]>
  244. If the key you wish to query by clashes with the name of another lookup, you
  245. need to use the :lookup:`hstorefield.contains` lookup instead.
  246. .. warning::
  247. Since any string could be a key in a hstore value, any lookup other than
  248. those listed below will be interpreted as a key lookup. No errors are
  249. raised. Be extra careful for typing mistakes, and always check your queries
  250. work as you intend.
  251. .. fieldlookup:: hstorefield.contains
  252. ``contains``
  253. ~~~~~~~~~~~~
  254. The :lookup:`contains` lookup is overridden on
  255. :class:`~django.contrib.postgres.fields.HStoreField`. The returned objects are
  256. those where the given ``dict`` of key-value pairs are all contained in the
  257. field. It uses the SQL operator ``@>``. For example::
  258. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador', 'owner': 'Bob'})
  259. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  260. >>> Dog.objects.create(name='Fred', data={})
  261. >>> Dog.objects.filter(data__contains={'owner': 'Bob'})
  262. <QuerySet [<Dog: Rufus>, <Dog: Meg>]>
  263. >>> Dog.objects.filter(data__contains={'breed': 'collie'})
  264. <QuerySet [<Dog: Meg>]>
  265. .. fieldlookup:: hstorefield.contained_by
  266. ``contained_by``
  267. ~~~~~~~~~~~~~~~~
  268. This is the inverse of the :lookup:`contains <hstorefield.contains>` lookup -
  269. the objects returned will be those where the key-value pairs on the object are
  270. a subset of those in the value passed. It uses the SQL operator ``<@``. For
  271. example::
  272. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador', 'owner': 'Bob'})
  273. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  274. >>> Dog.objects.create(name='Fred', data={})
  275. >>> Dog.objects.filter(data__contained_by={'breed': 'collie', 'owner': 'Bob'})
  276. <QuerySet [<Dog: Meg>, <Dog: Fred>]>
  277. >>> Dog.objects.filter(data__contained_by={'breed': 'collie'})
  278. <QuerySet [<Dog: Fred>]>
  279. .. fieldlookup:: hstorefield.has_key
  280. ``has_key``
  281. ~~~~~~~~~~~
  282. Returns objects where the given key is in the data. Uses the SQL operator
  283. ``?``. For example::
  284. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})
  285. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  286. >>> Dog.objects.filter(data__has_key='owner')
  287. <QuerySet [<Dog: Meg>]>
  288. .. fieldlookup:: hstorefield.has_any_keys
  289. ``has_any_keys``
  290. ~~~~~~~~~~~~~~~~
  291. Returns objects where any of the given keys are in the data. Uses the SQL
  292. operator ``?|``. For example::
  293. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})
  294. >>> Dog.objects.create(name='Meg', data={'owner': 'Bob'})
  295. >>> Dog.objects.create(name='Fred', data={})
  296. >>> Dog.objects.filter(data__has_any_keys=['owner', 'breed'])
  297. <QuerySet [<Dog: Rufus>, <Dog: Meg>]>
  298. .. fieldlookup:: hstorefield.has_keys
  299. ``has_keys``
  300. ~~~~~~~~~~~~
  301. Returns objects where all of the given keys are in the data. Uses the SQL operator
  302. ``?&``. For example::
  303. >>> Dog.objects.create(name='Rufus', data={})
  304. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  305. >>> Dog.objects.filter(data__has_keys=['breed', 'owner'])
  306. <QuerySet [<Dog: Meg>]>
  307. .. fieldlookup:: hstorefield.keys
  308. ``keys``
  309. ~~~~~~~~
  310. Returns objects where the array of keys is the given value. Note that the order
  311. is not guaranteed to be reliable, so this transform is mainly useful for using
  312. in conjunction with lookups on
  313. :class:`~django.contrib.postgres.fields.ArrayField`. Uses the SQL function
  314. ``akeys()``. For example::
  315. >>> Dog.objects.create(name='Rufus', data={'toy': 'bone'})
  316. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  317. >>> Dog.objects.filter(data__keys__overlap=['breed', 'toy'])
  318. <QuerySet [<Dog: Rufus>, <Dog: Meg>]>
  319. .. fieldlookup:: hstorefield.values
  320. ``values``
  321. ~~~~~~~~~~
  322. Returns objects where the array of values is the given value. Note that the
  323. order is not guaranteed to be reliable, so this transform is mainly useful for
  324. using in conjunction with lookups on
  325. :class:`~django.contrib.postgres.fields.ArrayField`. Uses the SQL function
  326. ``avalues()``. For example::
  327. >>> Dog.objects.create(name='Rufus', data={'breed': 'labrador'})
  328. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': 'Bob'})
  329. >>> Dog.objects.filter(data__values__contains=['collie'])
  330. <QuerySet [<Dog: Meg>]>
  331. ``JSONField``
  332. =============
  333. .. class:: JSONField(encoder=None, **options)
  334. A field for storing JSON encoded data. In Python the data is represented in
  335. its Python native format: dictionaries, lists, strings, numbers, booleans
  336. and ``None``.
  337. .. attribute:: encoder
  338. An optional JSON-encoding class to serialize data types not supported
  339. by the standard JSON serializer (``datetime``, ``uuid``, etc.). For
  340. example, you can use the
  341. :class:`~django.core.serializers.json.DjangoJSONEncoder` class or any
  342. other :py:class:`json.JSONEncoder` subclass.
  343. When the value is retrieved from the database, it will be in the format
  344. chosen by the custom encoder (most often a string), so you'll need to
  345. take extra steps to convert the value back to the initial data type
  346. (:meth:`Model.from_db() <django.db.models.Model.from_db>` and
  347. :meth:`Field.from_db_value() <django.db.models.Field.from_db_value>`
  348. are two possible hooks for that purpose). Your deserialization may need
  349. to account for the fact that you can't be certain of the input type.
  350. For example, you run the risk of returning a ``datetime`` that was
  351. actually a string that just happened to be in the same format chosen
  352. for ``datetime``\s.
  353. If you give the field a :attr:`~django.db.models.Field.default`, ensure
  354. it's a callable such as ``dict`` (for an empty default) or a callable that
  355. returns a dict (such as a function). Incorrectly using ``default={}``
  356. creates a mutable default that is shared between all instances of
  357. ``JSONField``.
  358. .. note::
  359. PostgreSQL has two native JSON based data types: ``json`` and ``jsonb``.
  360. The main difference between them is how they are stored and how they can be
  361. queried. PostgreSQL's ``json`` field is stored as the original string
  362. representation of the JSON and must be decoded on the fly when queried
  363. based on keys. The ``jsonb`` field is stored based on the actual structure
  364. of the JSON which allows indexing. The trade-off is a small additional cost
  365. on writing to the ``jsonb`` field. ``JSONField`` uses ``jsonb``.
  366. Querying ``JSONField``
  367. ----------------------
  368. We will use the following example model::
  369. from django.contrib.postgres.fields import JSONField
  370. from django.db import models
  371. class Dog(models.Model):
  372. name = models.CharField(max_length=200)
  373. data = JSONField()
  374. def __str__(self):
  375. return self.name
  376. .. fieldlookup:: jsonfield.key
  377. Key, index, and path lookups
  378. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  379. To query based on a given dictionary key, use that key as the lookup name::
  380. >>> Dog.objects.create(name='Rufus', data={
  381. ... 'breed': 'labrador',
  382. ... 'owner': {
  383. ... 'name': 'Bob',
  384. ... 'other_pets': [{
  385. ... 'name': 'Fishy',
  386. ... }],
  387. ... },
  388. ... })
  389. >>> Dog.objects.create(name='Meg', data={'breed': 'collie', 'owner': None})
  390. >>> Dog.objects.filter(data__breed='collie')
  391. <QuerySet [<Dog: Meg>]>
  392. Multiple keys can be chained together to form a path lookup::
  393. >>> Dog.objects.filter(data__owner__name='Bob')
  394. <QuerySet [<Dog: Rufus>]>
  395. If the key is an integer, it will be interpreted as an index lookup in an
  396. array::
  397. >>> Dog.objects.filter(data__owner__other_pets__0__name='Fishy')
  398. <QuerySet [<Dog: Rufus>]>
  399. If the key you wish to query by clashes with the name of another lookup, use
  400. the :lookup:`jsonfield.contains` lookup instead.
  401. If only one key or index is used, the SQL operator ``->`` is used. If multiple
  402. operators are used then the ``#>`` operator is used.
  403. To query for ``null`` in JSON data, use ``None`` as a value::
  404. >>> Dog.objects.filter(data__owner=None)
  405. <QuerySet [<Dog: Meg>]>
  406. To query for missing keys, use the ``isnull`` lookup::
  407. >>> Dog.objects.create(name='Shep', data={'breed': 'collie'})
  408. >>> Dog.objects.filter(data__owner__isnull=True)
  409. <QuerySet [<Dog: Shep>]>
  410. .. warning::
  411. Since any string could be a key in a JSON object, any lookup other than
  412. those listed below will be interpreted as a key lookup. No errors are
  413. raised. Be extra careful for typing mistakes, and always check your queries
  414. work as you intend.
  415. Containment and key operations
  416. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  417. .. fieldlookup:: jsonfield.contains
  418. .. fieldlookup:: jsonfield.contained_by
  419. .. fieldlookup:: jsonfield.has_key
  420. .. fieldlookup:: jsonfield.has_any_keys
  421. .. fieldlookup:: jsonfield.has_keys
  422. :class:`~django.contrib.postgres.fields.JSONField` shares lookups relating to
  423. containment and keys with :class:`~django.contrib.postgres.fields.HStoreField`.
  424. - :lookup:`contains <hstorefield.contains>` (accepts any JSON rather than
  425. just a dictionary of strings)
  426. - :lookup:`contained_by <hstorefield.contained_by>` (accepts any JSON
  427. rather than just a dictionary of strings)
  428. - :lookup:`has_key <hstorefield.has_key>`
  429. - :lookup:`has_any_keys <hstorefield.has_any_keys>`
  430. - :lookup:`has_keys <hstorefield.has_keys>`
  431. .. _range-fields:
  432. Range Fields
  433. ============
  434. There are five range field types, corresponding to the built-in range types in
  435. PostgreSQL. These fields are used to store a range of values; for example the
  436. start and end timestamps of an event, or the range of ages an activity is
  437. suitable for.
  438. All of the range fields translate to :ref:`psycopg2 Range objects
  439. <psycopg2:adapt-range>` in Python, but also accept tuples as input if no bounds
  440. information is necessary. The default is lower bound included, upper bound
  441. excluded; that is, ``[)``.
  442. ``IntegerRangeField``
  443. ---------------------
  444. .. class:: IntegerRangeField(**options)
  445. Stores a range of integers. Based on an
  446. :class:`~django.db.models.IntegerField`. Represented by an ``int4range`` in
  447. the database and a :class:`~psycopg2:psycopg2.extras.NumericRange` in
  448. Python.
  449. Regardless of the bounds specified when saving the data, PostgreSQL always
  450. returns a range in a canonical form that includes the lower bound and
  451. excludes the upper bound; that is ``[)``.
  452. ``BigIntegerRangeField``
  453. ------------------------
  454. .. class:: BigIntegerRangeField(**options)
  455. Stores a range of large integers. Based on a
  456. :class:`~django.db.models.BigIntegerField`. Represented by an ``int8range``
  457. in the database and a :class:`~psycopg2:psycopg2.extras.NumericRange` in
  458. Python.
  459. Regardless of the bounds specified when saving the data, PostgreSQL always
  460. returns a range in a canonical form that includes the lower bound and
  461. excludes the upper bound; that is ``[)``.
  462. ``DecimalRangeField``
  463. ---------------------
  464. .. class:: DecimalRangeField(**options)
  465. Stores a range of floating point values. Based on a
  466. :class:`~django.db.models.DecimalField`. Represented by a ``numrange`` in
  467. the database and a :class:`~psycopg2:psycopg2.extras.NumericRange` in
  468. Python.
  469. ``DateTimeRangeField``
  470. ----------------------
  471. .. class:: DateTimeRangeField(**options)
  472. Stores a range of timestamps. Based on a
  473. :class:`~django.db.models.DateTimeField`. Represented by a ``tstzrange`` in
  474. the database and a :class:`~psycopg2:psycopg2.extras.DateTimeTZRange` in
  475. Python.
  476. ``DateRangeField``
  477. ------------------
  478. .. class:: DateRangeField(**options)
  479. Stores a range of dates. Based on a
  480. :class:`~django.db.models.DateField`. Represented by a ``daterange`` in the
  481. database and a :class:`~psycopg2:psycopg2.extras.DateRange` in Python.
  482. Regardless of the bounds specified when saving the data, PostgreSQL always
  483. returns a range in a canonical form that includes the lower bound and
  484. excludes the upper bound; that is ``[)``.
  485. Querying Range Fields
  486. ---------------------
  487. There are a number of custom lookups and transforms for range fields. They are
  488. available on all the above fields, but we will use the following example
  489. model::
  490. from django.contrib.postgres.fields import IntegerRangeField
  491. from django.db import models
  492. class Event(models.Model):
  493. name = models.CharField(max_length=200)
  494. ages = IntegerRangeField()
  495. start = models.DateTimeField()
  496. def __str__(self):
  497. return self.name
  498. We will also use the following example objects::
  499. >>> import datetime
  500. >>> from django.utils import timezone
  501. >>> now = timezone.now()
  502. >>> Event.objects.create(name='Soft play', ages=(0, 10), start=now)
  503. >>> Event.objects.create(name='Pub trip', ages=(21, None), start=now - datetime.timedelta(days=1))
  504. and ``NumericRange``:
  505. >>> from psycopg2.extras import NumericRange
  506. Containment functions
  507. ~~~~~~~~~~~~~~~~~~~~~
  508. As with other PostgreSQL fields, there are three standard containment
  509. operators: ``contains``, ``contained_by`` and ``overlap``, using the SQL
  510. operators ``@>``, ``<@``, and ``&&`` respectively.
  511. .. fieldlookup:: rangefield.contains
  512. ``contains``
  513. ^^^^^^^^^^^^
  514. >>> Event.objects.filter(ages__contains=NumericRange(4, 5))
  515. <QuerySet [<Event: Soft play>]>
  516. .. fieldlookup:: rangefield.contained_by
  517. ``contained_by``
  518. ^^^^^^^^^^^^^^^^
  519. >>> Event.objects.filter(ages__contained_by=NumericRange(0, 15))
  520. <QuerySet [<Event: Soft play>]>
  521. The ``contained_by`` lookup is also available on the non-range field types:
  522. :class:`~django.db.models.SmallAutoField`,
  523. :class:`~django.db.models.AutoField`, :class:`~django.db.models.BigAutoField`,
  524. :class:`~django.db.models.SmallIntegerField`,
  525. :class:`~django.db.models.IntegerField`,
  526. :class:`~django.db.models.BigIntegerField`,
  527. :class:`~django.db.models.DecimalField`, :class:`~django.db.models.FloatField`,
  528. :class:`~django.db.models.DateField`, and
  529. :class:`~django.db.models.DateTimeField`. For example::
  530. >>> from psycopg2.extras import DateTimeTZRange
  531. >>> Event.objects.filter(start__contained_by=DateTimeTZRange(
  532. ... timezone.now() - datetime.timedelta(hours=1),
  533. ... timezone.now() + datetime.timedelta(hours=1),
  534. ... )
  535. <QuerySet [<Event: Soft play>]>
  536. .. versionchanged:: 3.1
  537. Support for :class:`~django.db.models.SmallAutoField`,
  538. :class:`~django.db.models.AutoField`,
  539. :class:`~django.db.models.BigAutoField`,
  540. :class:`~django.db.models.SmallIntegerField`, and
  541. :class:`~django.db.models.DecimalField` was added.
  542. .. fieldlookup:: rangefield.overlap
  543. ``overlap``
  544. ^^^^^^^^^^^
  545. >>> Event.objects.filter(ages__overlap=NumericRange(8, 12))
  546. <QuerySet [<Event: Soft play>]>
  547. Comparison functions
  548. ~~~~~~~~~~~~~~~~~~~~
  549. Range fields support the standard lookups: :lookup:`lt`, :lookup:`gt`,
  550. :lookup:`lte` and :lookup:`gte`. These are not particularly helpful - they
  551. compare the lower bounds first and then the upper bounds only if necessary.
  552. This is also the strategy used to order by a range field. It is better to use
  553. the specific range comparison operators.
  554. .. fieldlookup:: rangefield.fully_lt
  555. ``fully_lt``
  556. ^^^^^^^^^^^^
  557. The returned ranges are strictly less than the passed range. In other words,
  558. all the points in the returned range are less than all those in the passed
  559. range.
  560. >>> Event.objects.filter(ages__fully_lt=NumericRange(11, 15))
  561. <QuerySet [<Event: Soft play>]>
  562. .. fieldlookup:: rangefield.fully_gt
  563. ``fully_gt``
  564. ^^^^^^^^^^^^
  565. The returned ranges are strictly greater than the passed range. In other words,
  566. the all the points in the returned range are greater than all those in the
  567. passed range.
  568. >>> Event.objects.filter(ages__fully_gt=NumericRange(11, 15))
  569. <QuerySet [<Event: Pub trip>]>
  570. .. fieldlookup:: rangefield.not_lt
  571. ``not_lt``
  572. ^^^^^^^^^^
  573. The returned ranges do not contain any points less than the passed range, that
  574. is the lower bound of the returned range is at least the lower bound of the
  575. passed range.
  576. >>> Event.objects.filter(ages__not_lt=NumericRange(0, 15))
  577. <QuerySet [<Event: Soft play>, <Event: Pub trip>]>
  578. .. fieldlookup:: rangefield.not_gt
  579. ``not_gt``
  580. ^^^^^^^^^^
  581. The returned ranges do not contain any points greater than the passed range, that
  582. is the upper bound of the returned range is at most the upper bound of the
  583. passed range.
  584. >>> Event.objects.filter(ages__not_gt=NumericRange(3, 10))
  585. <QuerySet [<Event: Soft play>]>
  586. .. fieldlookup:: rangefield.adjacent_to
  587. ``adjacent_to``
  588. ^^^^^^^^^^^^^^^
  589. The returned ranges share a bound with the passed range.
  590. >>> Event.objects.filter(ages__adjacent_to=NumericRange(10, 21))
  591. <QuerySet [<Event: Soft play>, <Event: Pub trip>]>
  592. Querying using the bounds
  593. ~~~~~~~~~~~~~~~~~~~~~~~~~
  594. There are three transforms available for use in queries. You can extract the
  595. lower or upper bound, or query based on emptiness.
  596. .. fieldlookup:: rangefield.startswith
  597. ``startswith``
  598. ^^^^^^^^^^^^^^
  599. Returned objects have the given lower bound. Can be chained to valid lookups
  600. for the base field.
  601. >>> Event.objects.filter(ages__startswith=21)
  602. <QuerySet [<Event: Pub trip>]>
  603. .. fieldlookup:: rangefield.endswith
  604. ``endswith``
  605. ^^^^^^^^^^^^
  606. Returned objects have the given upper bound. Can be chained to valid lookups
  607. for the base field.
  608. >>> Event.objects.filter(ages__endswith=10)
  609. <QuerySet [<Event: Soft play>]>
  610. .. fieldlookup:: rangefield.isempty
  611. ``isempty``
  612. ^^^^^^^^^^^
  613. Returned objects are empty ranges. Can be chained to valid lookups for a
  614. :class:`~django.db.models.BooleanField`.
  615. >>> Event.objects.filter(ages__isempty=True)
  616. <QuerySet []>
  617. .. fieldlookup:: rangefield.lower_inc
  618. ``lower_inc``
  619. ^^^^^^^^^^^^^
  620. .. versionadded:: 3.1
  621. Returns objects that have inclusive or exclusive lower bounds, depending on the
  622. boolean value passed. Can be chained to valid lookups for a
  623. :class:`~django.db.models.BooleanField`.
  624. >>> Event.objects.filter(ages__lower_inc=True)
  625. <QuerySet [<Event: Soft play>, <Event: Pub trip>]>
  626. .. fieldlookup:: rangefield.lower_inf
  627. ``lower_inf``
  628. ^^^^^^^^^^^^^
  629. .. versionadded:: 3.1
  630. Returns objects that have unbounded (infinite) or bounded lower bound,
  631. depending on the boolean value passed. Can be chained to valid lookups for a
  632. :class:`~django.db.models.BooleanField`.
  633. >>> Event.objects.filter(ages__lower_inf=True)
  634. <QuerySet []>
  635. .. fieldlookup:: rangefield.upper_inc
  636. ``upper_inc``
  637. ^^^^^^^^^^^^^
  638. .. versionadded:: 3.1
  639. Returns objects that have inclusive or exclusive upper bounds, depending on the
  640. boolean value passed. Can be chained to valid lookups for a
  641. :class:`~django.db.models.BooleanField`.
  642. >>> Event.objects.filter(ages__upper_inc=True)
  643. <QuerySet []>
  644. .. fieldlookup:: rangefield.upper_inf
  645. ``upper_inf``
  646. ^^^^^^^^^^^^^
  647. .. versionadded:: 3.1
  648. Returns objects that have unbounded (infinite) or bounded upper bound,
  649. depending on the boolean value passed. Can be chained to valid lookups for a
  650. :class:`~django.db.models.BooleanField`.
  651. >>> Event.objects.filter(ages__upper_inf=True)
  652. <QuerySet [<Event: Pub trip>]>
  653. Defining your own range types
  654. -----------------------------
  655. PostgreSQL allows the definition of custom range types. Django's model and form
  656. field implementations use base classes below, and psycopg2 provides a
  657. :func:`~psycopg2:psycopg2.extras.register_range` to allow use of custom range
  658. types.
  659. .. class:: RangeField(**options)
  660. Base class for model range fields.
  661. .. attribute:: base_field
  662. The model field class to use.
  663. .. attribute:: range_type
  664. The psycopg2 range type to use.
  665. .. attribute:: form_field
  666. The form field class to use. Should be a subclass of
  667. :class:`django.contrib.postgres.forms.BaseRangeField`.
  668. .. class:: django.contrib.postgres.forms.BaseRangeField
  669. Base class for form range fields.
  670. .. attribute:: base_field
  671. The form field to use.
  672. .. attribute:: range_type
  673. The psycopg2 range type to use.
  674. Range operators
  675. ---------------
  676. .. versionadded:: 3.0
  677. .. class:: RangeOperators
  678. PostgreSQL provides a set of SQL operators that can be used together with the
  679. range data types (see `the PostgreSQL documentation for the full details of
  680. range operators <https://www.postgresql.org/docs/current/
  681. functions-range.html#RANGE-OPERATORS-TABLE>`_). This class is meant as a
  682. convenient method to avoid typos. The operator names overlap with the names of
  683. corresponding lookups.
  684. .. code-block:: python
  685. class RangeOperators:
  686. EQUAL = '='
  687. NOT_EQUAL = '<>'
  688. CONTAINS = '@>'
  689. CONTAINED_BY = '<@'
  690. OVERLAPS = '&&'
  691. FULLY_LT = '<<'
  692. FULLY_GT = '>>'
  693. NOT_LT = '&>'
  694. NOT_GT = '&<'
  695. ADJACENT_TO = '-|-'
  696. RangeBoundary() expressions
  697. ---------------------------
  698. .. versionadded:: 3.0
  699. .. class:: RangeBoundary(inclusive_lower=True, inclusive_upper=False)
  700. .. attribute:: inclusive_lower
  701. If ``True`` (default), the lower bound is inclusive ``'['``, otherwise
  702. it's exclusive ``'('``.
  703. .. attribute:: inclusive_upper
  704. If ``False`` (default), the upper bound is exclusive ``')'``, otherwise
  705. it's inclusive ``']'``.
  706. A ``RangeBoundary()`` expression represents the range boundaries. It can be
  707. used with a custom range functions that expected boundaries, for example to
  708. define :class:`~django.contrib.postgres.constraints.ExclusionConstraint`. See
  709. `the PostgreSQL documentation for the full details <https://www.postgresql.org/
  710. docs/current/rangetypes.html#RANGETYPES-INCLUSIVITY>`_.