6.7. ORM Get
.get()
.first()
.last()
.earliest()
.latest()
.get_or_create()
6.7.1. Get
>>> Customer.objects.get(pk=1)
<Customer: Mark Watney>
6.7.2. Errors
>>> Customer.objects.get(id=999)
Traceback (most recent call last):
shop.models.customer.Customer.DoesNotExist: Customer matching query does not exist.
>>> Customer.objects.get(firstname='Mark')
Traceback (most recent call last):
# shop.models.customer.Customer.MultipleObjectsReturned: get() returned more than one Customer -- it returned 2!
6.7.3. First
>>> Customer.objects.first()
<Customer: Mark Watney>
6.7.4. Last
>>> Customer.objects.last()
<Customer: Melissa Lewis>
6.7.5. Earliest
>>> Customer.objects.earliest('created_date')
<Customer: Mark Watney>
6.7.6. Latest
>>> Customer.objects.latest('created_date')
<Customer: Melissa Lewis>
6.7.7. Try Get
>>> try:
... user = Customer.objects.get(firstname='Mark', lastname='Lewis')
... except Customer.DoesNotExist:
... print('Sorry, user does not exist')
... except Customer.MultipleObjectsReturned:
... print('Sorry, we have more than one user with this name')
Sorry, user does not exist
6.7.8. Get or Create
>>> mark = Customer.objects.get_or_create(
... firstname='Mark',
... lastname='Watney',
... defaults={'is_verified': True},
... )
6.7.9. Get or 404
Shortcut for getting an object and raising a 404 error if it doesn't exist
django.shortcuts.get_object_or_404
>>> from django.shortcuts import get_object_or_404
>>>
>>>
>>> def my_view(request):
... mark = get_object_or_404(Customer, pk=1)
... return dict(mark)
6.7.10. 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select one Customer
# Where `pk` equals `1`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz jednego klienta
# Gdzie `pk` równa się `1`
# %% Hints
# - `.get()`
# %% 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 QuerySet, \
'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 query:
# Select one Customer
# Where `pk` equals `1`
# 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select one Customer
# Where `firstname` equals `Mark`
# And `lastname` equals `Watney`
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz jednego klienta
# Gdzie `firstname` równa się `Mark`
# Oraz `lastname` równa się `Watney`
# %% Hints
# - `.get()`
# %% 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 query:
# Select one Customer
# Where `firstname` equals `Mark`
# And `lastname` equals `Watney`
# 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select first Customer
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz pierwszego klienta
# %% Hints
# - `.first()`
# %% 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 query:
# Select first Customer
# 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select last Customer
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz ostatniego klienta
# %% Hints
# - `.last()`
# %% 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: 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 query:
# Select last Customer
# 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select earliest created Customer
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz najwcześniej stworzonego klienta
# %% Hints
# - `.earliest()`
# %% 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 query:
# Select earliest created Customer
# 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 Get
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of query:
# Select latest created Customer
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania:
# Wybierz najpóźniej stworzonego klienta
# %% Hints
# - `.latest()`
# %% 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: 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 query:
# Select latest created Customer
# type: Customer
result = ...