6.5. ORM Delete
.delete()
6.5.1. Delete One
>>>
... Customer.objects.get(firstname='Mark', lastname='Watney').delete()
6.5.2. Delete Many
>>> Customer.objects.filter(firstname='Mark').delete()
6.5.3. 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 Create
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for
# delete one `Customer`:
# - firstname: John
# - lastname: Doe
# 2. Use `.delete()` method
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla
# skasowaniem jednego `Customer`a:
# - firstname: John
# - lastname: Doe
# 2. Użyj metody `.delete()`
# %% Hints
# - `.filter()`
# - `.delete()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
>>> result
(1, {'shop.Customer': 1})
>>> assert not Customer.objects.filter(firstname='John', lastname='Doe').exists()
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
from shop.models import Customer
Customer.objects.create(firstname='John', lastname='Doe')
# Define variable `result` with result of ORM call for
# delete one `Customer`:
# - firstname: John
# - lastname: Doe
# Use `.delete()` method
# type: tuple[int, dict[str,int]]
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 Create
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2
# %% English
# 0. Use `myproject.shop`
# 1. Define variable `result` with result of ORM call for
# delete many `Customer`s:
# - firstname: John, lastname: Doe
# - firstname: Jane, lastname: Doe
# 2. Use `.delete()` method
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zdefiniuj zmienną `result` z wynikiem zapytania ORM dla
# skasowaniem wielu `Customer`ów:
# - firstname: John, lastname: Doe
# - firstname: Jane, lastname: Doe
# 2. Użyj metody `.delete()`
# %% Hints
# - `.filter()`
# - `.delete()`
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
>>> result
(2, {'shop.Customer': 2})
>>> assert not Customer.objects.filter(firstname='John', lastname='Doe').exists()
>>> assert not Customer.objects.filter(firstname='Jane', lastname='Doe').exists()
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
from shop.models import Customer
Customer.objects.create(firstname='John', lastname='Doe')
Customer.objects.create(firstname='Jane', lastname='Doe')
# Define variable `result` with result of ORM call for
# delete many `Customer`s:
# - firstname: John, lastname: Doe
# - firstname: Jane, lastname: Doe
# Use `.delete()` method
# type: tuple[int, dict[str,int]]
result = ...