fields.txt 29 KB

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