From 94b6f101f7dc363a8e71593570b17527dbb9f77f Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Sat, 30 Dec 2023 07:24:30 +0000 Subject: Fixed #29049 -- Added slicing notation to F expressions. Co-authored-by: Priyansh Saxena Co-authored-by: Niclas Olofsson Co-authored-by: David Smith Co-authored-by: Mariusz Felisiak Co-authored-by: Abhinav Yadav --- docs/ref/models/expressions.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'docs/ref') 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()`` -- cgit v1.3