summaryrefslogtreecommitdiff
path: root/docs
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
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')
-rw-r--r--docs/ref/models/expressions.txt22
-rw-r--r--docs/releases/5.1.txt8
2 files changed, 30 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()``
diff --git a/docs/releases/5.1.txt b/docs/releases/5.1.txt
index cc72346eef..b825e9be4f 100644
--- a/docs/releases/5.1.txt
+++ b/docs/releases/5.1.txt
@@ -184,6 +184,14 @@ Models
* :meth:`.QuerySet.order_by` now supports ordering by annotation transforms
such as ``JSONObject`` keys and ``ArrayAgg`` indices.
+* :class:`F() <django.db.models.F>` and :class:`OuterRef()
+ <django.db.models.OuterRef>` expressions that output
+ :class:`~django.db.models.CharField`, :class:`~django.db.models.EmailField`,
+ :class:`~django.db.models.SlugField`, :class:`~django.db.models.URLField`,
+ :class:`~django.db.models.TextField`, or
+ :class:`~django.contrib.postgres.fields.ArrayField` can now be :ref:`sliced
+ <slicing-using-f>`.
+
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~