8.11. ORM F

>>> from django.db.models import F
>>>
>>> Customer.objects.all().update(age=F('age')+1)

8.11.1. Filters can reference fields on the model

from django.db.models import F

Entry.objects.filter(n_comments__gt=F('n_pingbacks'))
Entry.objects.filter(n_comments__gt=F('n_pingbacks') * 2)
Entry.objects.filter(rating__lt=F('n_comments') + F('n_pingbacks'))
Entry.objects.filter(authors__name=F('blog__name'))
from datetime import timedelta

Entry.objects.filter(mod_date__gt=F('pub_date') + timedelta(days=3))

8.11.2. F() expressions

An F() object represents the value of a model field or annotated column. It makes it possible to refer to model field values and perform database operations using them without actually having to pull them out of the database into Python memory.

Iris.objects.all().update(petal_length=F('petal_length') + 1)

8.11.3. Assignments

# doctest: +SKIP_FILE
# %% About
# - Name: Database ORM F
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 0. Use `myproject.shop`
# 1. Increment `price` of all Products by 10
# 2. Use `F` object

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zwiększ cenę (`price`) wszystkich Produktów o 10
# 2. Użyj obiektu `F`

# %% Hints
# - `F()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is int, \
'Variable `result` has an invalid type; expected: `int`.'

>>> from pprint import pprint
>>> pprint(result)
25
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
from django.db.models import F
from shop.models import Product

# %% Types
result: int

# %% Data

# %% Result
result = ...

# doctest: +SKIP_FILE
# %% About
# - Name: Database ORM F
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% English
# 0. Use `myproject.shop`
# 1. Decrement `price` of all Products by 10
# 2. Use `F` object

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zmniejsz cenę (`price`) wszystkich Produktów o 10
# 2. Użyj obiektu `F`

# %% Hints
# - `F()`

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 12), \
'Python has an is invalid version; expected: `3.12` or newer.'

>>> assert 'result' in globals(), \
'Variable `result` is not defined; assign result of your program to it.'

>>> assert result is not Ellipsis, \
'Variable `result` has an invalid value; assign result of your program to it.'

>>> assert type(result) is int, \
'Variable `result` has an invalid type; expected: `int`.'

>>> from pprint import pprint
>>> pprint(result)
25
"""

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`

# %% Imports
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
from django.db.models import F
from shop.models import Product

# %% Types
result: int

# %% Data

# %% Result
result = ...