summaryrefslogtreecommitdiff
path: root/docs/ref
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2023-12-30 07:24:30 +0000
committerGitHub <noreply@github.com>2023-12-30 08:24:30 +0100
commit94b6f101f7dc363a8e71593570b17527dbb9f77f (patch)
tree431ddcb41b024af3a4fba136b53a210ad390cf4c /docs/ref
parent561e16d6a7f07d8cadbdb6c72c68934dcd087d18 (diff)
Fixed #29049 -- Added slicing notation to F expressions.
Co-authored-by: Priyansh Saxena <askpriyansh@gmail.com> Co-authored-by: Niclas Olofsson <n@niclasolofsson.se> Co-authored-by: David Smith <smithdc@gmail.com> Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com> Co-authored-by: Abhinav Yadav <abhinav.sny.2002@gmail.com>
Diffstat (limited to 'docs/ref')
-rw-r--r--docs/ref/models/expressions.txt22
1 files changed, 22 insertions, 0 deletions
diff --git a/docs/ref/models/expressions.txt b/docs/ref/models/expressions.txt
index 9d85442d9c..67baef7dfc 100644
--- a/docs/ref/models/expressions.txt
+++ b/docs/ref/models/expressions.txt
@@ -183,6 +183,28 @@ the field value of each one, and saving each one back to the database::
* getting the database, rather than Python, to do work
* reducing the number of queries some operations require
+.. _slicing-using-f:
+
+Slicing ``F()`` expressions
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. versionadded:: 5.1
+
+For string-based fields, text-based fields, and
+:class:`~django.contrib.postgres.fields.ArrayField`, you can use Python's
+array-slicing syntax. The indices are 0-based and the ``step`` argument to
+``slice`` is not supported. For example:
+
+.. code-block:: pycon
+
+ >>> # Replacing a name with a substring of itself.
+ >>> writer = Writers.objects.get(name="Priyansh")
+ >>> writer.name = F("name")[1:5]
+ >>> writer.save()
+ >>> writer.refresh_from_db()
+ >>> writer.name
+ 'riya'
+
.. _avoiding-race-conditions-using-f:
Avoiding race conditions using ``F()``