6.8. ORM Filter

  • .filter()

  • .exclude()

  • .complex_filter()

  • .extra()

  • .filter()

  • .reverse()

  • .union()

6.8.1. Filter by One Field

>>> Customer.objects.filter(firstname='Mark')
<QuerySet [<Customer: Mark Watney>]>

6.8.2. Filter by Many Fields

>>> Customer.objects.filter(firstname='Mark', lastname='Watney')
<QuerySet [<Customer: Mark Watney>]>

6.8.3. Exclude

>>> Customer.objects.exclude(lastname='Watney')
<QuerySet [<Customer: Rick Martinez>, <Customer: Alex Vogel>, <Customer: Beth Johansson>, <Customer: Chris Beck>, <Customer: Melissa Lewis>]>
>>> Customer.objects.filter(firstname='Mark').exclude(lastname='Watney')
<QuerySet []>

6.8.4. Compose Query

>>> result = Customer.objects
>>> result = result.filter(firstname='Mark')
>>> result = result.filter(birthdate='2000-01-02')
>>> result = result.exclude(lastname='Lewis')
>>>
>>> result
<QuerySet [<Customer: Mark Watney>]>

6.8.5. Chain Query

  • \\ - line continuation character

  • (...) - parentheses

>>> result = Customer.objects \
...          .filter(firstname='Mark') \
...          .filter(birthdate='2000-01-02') \
...          .exclude(lastname='Lewis')
>>>
>>> result
<QuerySet [<Customer: Mark Watney>]>
>>> result = (
...     Customer.objects
...     .filter(firstname='Mark')
...     .filter(birthdate='2000-01-02')
...     .exclude(lastname='Lewis')
... )
>>>
>>> result
<QuerySet [<Customer: Mark Watney>]>

6.8.6. Getitem

>>> Customer.objects.filter(firstname='Mark')[1]
<Customer: Mark Watney>

6.8.7. Slice

>>> Customer.objects.filter(firstname='Mark')[:3]
<QuerySet [<Customer: Mark Watney>]>
>>> Customer.objects.filter(firstname='Mark')[1:3]
<QuerySet [<Customer: Mark Watney>]>
>>> Customer.objects.filter(firstname='Mark')[:3]
<QuerySet [<Customer: Mark Watney>, <Customer: Mark Doe>, <Customer: Mark Smith>]>

6.8.8. Assignments

# doctest: +SKIP_FILE
# %% 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

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

# %% About
# - Name: Database ORM Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for:
#    Select all Customers
#    Where `firstname` is `Mark`

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla:
#    Wybierz wszystkich klientów
#    Gdzie `firstname` to `Mark`

# %% Hints
# - `.filter()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> from django.db.models.query import QuerySet
>>> assert type(result) is QuerySet, \
'Variable `result` has invalid type, should be QuerySet'

>>> from pprint import pprint
>>> pprint(result)
<QuerySet [<Customer: Mark Watney>]>
"""

# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()

from shop.models import Customer


# Define variable `result` with result of ORM call for:
# Select all Customers
# Where `firstname` is `Mark`
# type: QuerySet
result = ...


# doctest: +SKIP_FILE
# %% 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

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

# %% About
# - Name: Database ORM Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for:
#    Select all Customers
#    Where `firstname` is `Mark`
#    And `lastname` is `Watney`

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla:
#    Wybierz wszystkich klientów
#    Gdzie `firstname` to `Mark`
#    Oraz `lastname` to `Watney`

# %% Hints
# - `.filter()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> from django.db.models.query import QuerySet
>>> assert type(result) is QuerySet, \
'Variable `result` has invalid type, should be QuerySet'

>>> from pprint import pprint
>>> pprint(result)
<QuerySet [<Customer: Mark Watney>]>
"""

# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()

from shop.models import Customer


# Define variable `result` with result of ORM call for:
# Select all Customers
# Where `firstname` is `Mark`
# And `lastname` is `Watney`
# type: QuerySet
result = ...


# doctest: +SKIP_FILE
# %% 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

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

# %% About
# - Name: Database ORM Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for:
#    Select all Customers
#    Where `firstname` is not `Mark`
#    And `lastname` is not `Watney`

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla:
#    Wybierz wszystkich klientów
#    Gdzie `firstname` to nie `Mark`
#    Oraz `lastname` to nie `Watney`

# %% Hints
# - `.exclude()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> from django.db.models.query import QuerySet
>>> assert type(result) is QuerySet, \
'Variable `result` has invalid type, should be QuerySet'

>>> from pprint import pprint
>>> pprint(result)
<QuerySet [<Customer: Melissa Lewis>, <Customer: Rick Martinez>, <Customer: Alex Vogel>, <Customer: Beth Johanssen>, <Customer: Chris Beck>]>
"""

# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()

from shop.models import Customer


# Define variable `result` with result of ORM call for:
# Select all Customers
# Where `firstname` is not `Mark`
# And `lastname` is not `Watney`
# type: QuerySet
result = ...


# doctest: +SKIP_FILE
# %% 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

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

# %% About
# - Name: Database ORM Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for:
#    Select first Customer
# 2. Do not use `first()` method

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla:
#    Wybierz pierwszego klienta
# 2. Nie używaj metody `first()`

# %% Hints
# - `.all()`
# - getitem

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'
>>> assert type(result) is Customer, \
'Variable `result` has invalid type, should be Customer'

>>> from pprint import pprint
>>> pprint(result)
<Customer: Mark Watney>
"""

# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()

from shop.models import Customer


# Define variable `result` with result of ORM call for:
# Select first Customer
# Do not use `first()` method
# type: Customer
result = ...


# doctest: +SKIP_FILE
# %% 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

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

# %% About
# - Name: Database ORM Filter
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for:
#    Select first three Customers
# 2. Do not use `first()` method

# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla:
#    Wybierz pierwszych trzech klientów
# 2. Nie używaj metody `first()`

# %% Hints
# - `.all()`
# - slice

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> assert result is not Ellipsis, \
'Assign your result to variable `result`'

>>> from django.db.models.query import QuerySet
>>> assert type(result) is QuerySet, \
'Variable `result` has invalid type, should be QuerySet'

>>> from pprint import pprint
>>> pprint(result)
<QuerySet [<Customer: Mark Watney>, <Customer: Melissa Lewis>, <Customer: Rick Martinez>]>
"""

# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()

from shop.models import Customer


# Define variable `result` with result of ORM call for:
# Select first three Customers
# Do not use `first()` method
# type: QuerySet
result = ...