6.1. ORM About
6.1.1. Create Objects
.bulk_create()
.create()
.get_or_create()
.save()
.update_or_create()
6.1.2. Update Objects
.bulk_update()
.save()
.select_for_update()
.update()
.update_or_create()
6.1.3. Delete Objects
.delete()
6.1.4. Get One Result
.earliest()
.first()
.get()
.get_or_create()
.last()
.latest()
6.1.5. Get Many Result
.all()
.complex_filter()
.extra()
.filter()
.reverse()
.union()
6.1.6. Narrow Results
.exclude()
.intersection()
.none()
.only()
.values()
.values_list()
result[1:]
result[1]
result[::2]
6.1.7. Check Results
.check()
.exist()
.exists()
.explain()
6.1.8. Order Results
.order_by()
6.1.9. Performance
.prefetch_related()
.select_related()
6.1.10. Functions
.aggregate()
.alias()
.annotate()
.count()
.distinct()
.using()
6.1.11. Other
.dates()
.datetimes()
.difference()
.in_bulk()
.raw()
6.1.12. Lookup
Empty:
__isnull
Sequences:
__in
Strings:
__contains
- case sensitive__endswith
- case sensitive__exact
- case sensitive (default)__icontains
- case insensitive__iendswith
- case insensitive__iexact
- case insensitive__istartswith
- case insensitive__startswith
- case sensitive
Numeric, Dates:
__eq
- equals__gt
- greater than__gte
- greater or equal than__lt
- less than__lte
- less or eaquan than
Dates:
__year
__month
__day
__hour
__minute
__second
__microsecond
__range
- between two dates__in
- a list of dates
6.1.13. Comparing objects
>>>
... my == other
... my.id == other.id
>>>
... my == other
... my.name == other.name
6.1.14. Assignments
# TODO: Create Tests
# 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 Import
# - Difficulty: easy
# - Lines: 2
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Import data from `PRODUCTS`
# 2. Non-functional requirements:
# - You may use any Python standard library module
# - You can use Django with migrations
# - Do not install any additional packages
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zaimportuj dane z `PRODUCTS`
# 2. Wymagania niefunkcjonalne:
# - Możesz użyć dowolnego modułu z biblioteki standardowej
# - Możesz użyć Django
# - Nie instaluj ani nie używaj dodatkowych pakietów
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
PRODUCTS = [
{"barcode": "5039271113244", "name": "Alfa", "price": "123.00"},
{"barcode": "5202038482222", "name": "Bravo", "price": "312.22"},
{"barcode": "5308443764554", "name": "Charlie", "price": "812.00"},
{"barcode": "5439667086587", "name": "Delta", "price": "332.18"},
{"barcode": "5527865721147", "name": "Echo", "price": "114.00"},
{"barcode": "5535686226512", "name": "Foxtrot", "price": "99.12"},
{"barcode": "5721668602638", "name": "Golf", "price": "123.00"},
{"barcode": "5776136485596", "name": "Hotel", "price": "444.40"},
{"barcode": "5863969679442", "name": "India", "price": "674.21"},
{"barcode": "5908105406923", "name": "Juliet", "price": "324.00"},
{"barcode": "5957751061635", "name": "Kilo", "price": "932.20"},
{"barcode": "6190780033092", "name": "Lima", "price": "128.00"},
{"barcode": "6512625994397", "name": "Mike", "price": "91.00"},
{"barcode": "6518235371269", "name": "November", "price": "12.00"},
{"barcode": "6565923118590", "name": "Oscar", "price": "43.10"},
{"barcode": "6650630136545", "name": "Papa", "price": "112.00"},
{"barcode": "6692669560199", "name": "Quebec", "price": "997.10"},
{"barcode": "6711341590108", "name": "Romeo", "price": "1337.00"},
{"barcode": "6816011714454", "name": "Sierra", "price": "998.10"},
{"barcode": "7050114819954", "name": "Tango", "price": "123.00"},
{"barcode": "7251625012784", "name": "Uniform", "price": "564.99"},
{"barcode": "7251925199277", "name": "Victor", "price": "990.50"},
{"barcode": "7283004100423", "name": "Whisky", "price": "881.89"},
{"barcode": "7309682004683", "name": "X-Ray", "price": "123.63"},
{"barcode": "7324670042560", "name": "Zulu", "price": "311.00"}
]
from shop.models import Product
# Clean environment before inserting data
Product.objects.all().delete()
# Import data from `PRODUCTS`
# You may use any Python standard library module
# You can use Django with migrations
# Do not install any additional packages
...
# TODO: Create Tests
# 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 Import
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Import data from `CUSTOMERS`
# 2. Non-functional requirements:
# - You may use any Python standard library module
# - You can use Django with migrations
# - Do not install any additional packages
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zaimportuj dane z `CUSTOMERS`
# 2. Wymagania niefunkcjonalne:
# - Możesz użyć dowolnego modułu z biblioteki standardowej
# - Możesz użyć Django
# - Nie instaluj ani nie używaj dodatkowych pakietów
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
CUSTOMERS = [
{
"firstname": "Mark",
"lastname": "Watney",
"birthdate": "1994-10-12",
"gender": "male",
"tax_number": "94101212345",
"email": "mwatney@nasa.gov",
"phone": "+1 (234) 555-0000"
},
{
"firstname": "Melissa",
"lastname": "Lewis",
"birthdate": "1995-07-15",
"gender": "female",
"tax_number": "95071512345",
"email": "mlewis@nasa.gov",
"phone": "+1 (234) 555-0001"
},
{
"firstname": "Rick",
"lastname": "Martinez",
"birthdate": "1996-01-21",
"gender": "male",
"tax_number": "96012112345",
"email": "rmartinez@nasa.gov",
"phone": "+1 (234) 555-0010"
},
{
"firstname": "Alex",
"lastname": "Vogel",
"birthdate": "1994-11-15",
"gender": "male",
"tax_number": "94111512345",
"email": "avogel@esa.int",
"phone": "+49 (234) 555-0011"
},
{
"firstname": "Beth",
"lastname": "Johanssen",
"birthdate": "2006-05-09",
"gender": "female",
"tax_number": "06250912345",
"email": "bjohanssen@nasa.gov",
"phone": "+1 (234) 555-0100"
},
{
"firstname": "Chris",
"lastname": "Beck",
"birthdate": "1999-08-02",
"gender": "male",
"tax_number": "99080212345",
"email": "cbeck@nasa.gov",
"phone": "+1 (234) 555-0101"
}
]
from shop.models import Customer
# Clean environment before inserting data
Customer.objects.all().delete()
# Import data from `CUSTOMERS`
# You may use any Python standard library module
# You can use Django with migrations
# Do not install any additional packages
...
# TODO: Create Tests
# 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 Import
# - Difficulty: easy
# - Lines: 3
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Import data from `ADDRESSES`
# 2. Non-functional requirements:
# - You may use any Python standard library module
# - You can use Django with migrations
# - Do not install any additional packages
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zaimportuj dane z `ADDRESSES`
# 2. Wymagania niefunkcjonalne:
# - Możesz użyć dowolnego modułu z biblioteki standardowej
# - Możesz użyć Django
# - Nie instaluj ani nie używaj dodatkowych pakietów
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
ADDRESSES = [
{
"customer": "mwatney@nasa.gov",
"type": "billing",
"street": "2101 E NASA Pkwy",
"city": "Houston",
"postcode": "77058",
"region": "Texas",
"country": "USA"
},
{
"customer": "mwatney@nasa.gov",
"type": "shipping",
"street": "",
"city": "Kennedy Space Center",
"postcode": "32899",
"region": "Florida",
"country": "USA"
},
{
"customer": "mlewis@nasa.gov",
"type": "shipping",
"street": "Kamienica Pod św. Janem Kapistranem",
"city": "Kraków",
"postcode": "31008",
"region": "Małopolskie",
"country": "Poland"
},
{
"customer": "rmartinez@nasa.gov",
"type": "billing",
"street": "",
"city": "Звёздный городо́к",
"postcode": "141160",
"region": "Московская область",
"country": "Россия"
},
{
"customer": "rmartinez@nasa.gov",
"type": "shipping",
"street": "",
"city": "Космодро́м Байкону́р",
"postcode": "",
"region": "Кызылординская область",
"country": "Қазақстан"
},
{
"customer": "avogel@esa.int",
"type": "shipping",
"street": "Linder Hoehe",
"city": "Cologne",
"postcode": "51147",
"region": "North Rhine-Westphalia",
"country": "Germany"
},
{
"customer": "bjohanssen@nasa.gov",
"type": "shipping",
"street": "2825 E Ave P",
"city": "Palmdale",
"postcode": "93550",
"region": "California",
"country": "USA"
},
{
"customer": "cbeck@nasa.gov",
"type": "shipping",
"street": "4800 Oak Grove Dr",
"city": "Pasadena",
"postcode": "91109",
"region": "California",
"country": "USA"
}
]
from shop.models import Customer, Address
# Clean environment before inserting data
Address.objects.all().delete()
# Import data from `ADDRESSES`
# You may use any Python standard library module
# You can use Django with migrations
# Do not install any additional packages
...
# TODO: Create Tests
# 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 Import
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5
# %% English
# 0. Use `myproject.shop`
# 1. Import data from `ORDERS`
# 2. Non-functional requirements:
# - You may use any Python standard library module
# - You can use Django with migrations
# - Do not install any additional packages
# %% Polish
# 0. Użyj `myproject.shop`
# 1. Zaimportuj dane z `ORDERS`
# 2. Wymagania niefunkcjonalne:
# - Możesz użyć dowolnego modułu z biblioteki standardowej
# - Możesz użyć Django
# - Nie instaluj ani nie używaj dodatkowych pakietów
# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'
"""
# Required for Django to work
import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
import django; django.setup()
ORDERS = [
{"customer": "mwatney@nasa.gov", "product": "Sierra"},
{"customer": "mwatney@nasa.gov", "product": "Victor"},
{"customer": "bjohanssen@nasa.gov", "product": "Delta"},
{"customer": "mlewis@nasa.gov", "product": "November"},
{"customer": "rmartinez@nasa.gov", "product": "Mike"},
{"customer": "mwatney@nasa.gov", "product": "Bravo"},
{"customer": "mwatney@nasa.gov", "product": "Kilo"},
{"customer": "avogel@esa.int", "product": "Victor"},
{"customer": "bjohanssen@nasa.gov", "product": "Romeo"},
{"customer": "bjohanssen@nasa.gov", "product": "Whisky"},
{"customer": "cbeck@nasa.gov", "product": "Zulu"},
{"customer": "mwatney@nasa.gov", "product": "Romeo"},
{"customer": "avogel@esa.int", "product": "Romeo"},
{"customer": "bjohanssen@nasa.gov", "product": "Victor"},
{"customer": "bjohanssen@nasa.gov", "product": "Whisky"},
{"customer": "mlewis@nasa.gov", "product": "Whisky"},
{"customer": "rmartinez@nasa.gov", "product": "Mike"},
{"customer": "mwatney@nasa.gov", "product": "November"},
{"customer": "mwatney@nasa.gov", "product": "Kilo"},
{"customer": "avogel@esa.int", "product": "Bravo"},
{"customer": "bjohanssen@nasa.gov", "product": "X-Ray"},
{"customer": "avogel@esa.int", "product": "Romeo"},
{"customer": "bjohanssen@nasa.gov", "product": "Victor"},
{"customer": "bjohanssen@nasa.gov", "product": "India"},
{"customer": "mlewis@nasa.gov", "product": "Juliet"},
{"customer": "rmartinez@nasa.gov", "product": "Foxtrot"},
{"customer": "avogel@esa.int", "product": "Victor"},
{"customer": "bjohanssen@nasa.gov", "product": "Romeo"},
{"customer": "bjohanssen@nasa.gov", "product": "Whisky"},
{"customer": "cbeck@nasa.gov", "product": "Zulu"},
{"customer": "mwatney@nasa.gov", "product": "Alfa"},
{"customer": "avogel@esa.int", "product": "Romeo"},
{"customer": "bjohanssen@nasa.gov", "product": "Quebec"},
]
from shop.models import Order, Customer, Product
# Clean environment before inserting data
Order.objects.all().delete()
# Import data from `ORDERS`
# You may use any Python standard library module
# You can use Django with migrations
# Do not install any additional packages
...