revert docs changes, pin databases, fix mypy

This commit is contained in:
collerek
2022-01-14 17:36:18 +01:00
parent bf3b36194b
commit 53a2636421
13 changed files with 61 additions and 119 deletions

View File

@ -26,9 +26,7 @@ class Course(ormar.Model):
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey(
Department, related_name="my_courses"
)
department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses")
department = Department(name="Science")

View File

@ -8,9 +8,7 @@ metadata = sqlalchemy.MetaData()
class Course(ormar.Model):
class Meta(
ormar.ModelMeta
): # note you don't have to subclass - but it's recommended for ide completion and mypy
class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
database = database
metadata = metadata

View File

@ -16,5 +16,4 @@ class Course(ormar.Model):
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
c1 = Course()
c1 = Course()

View File

@ -15,17 +15,14 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.update(each=True, genre="Fiction")
all_books = await Book.objects.filter(genre="Fiction").all()
await Book.objects.update(each=True, genre='Fiction')
all_books = await Book.objects.filter(genre='Fiction').all()
assert len(all_books) == 3

View File

@ -15,23 +15,18 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
# if not exist the instance will be persisted in db
vol2 = await Book.objects.update_or_create(
title="Volume II", author="Anonymous", genre="Fiction"
)
vol2 = await Book.objects.update_or_create(title="Volume II", author='Anonymous', genre='Fiction')
assert await Book.objects.count() == 1
# if pk or pkname passed in kwargs (like id here) the object will be updated
assert await Book.objects.update_or_create(id=vol2.id, genre="Historic")
assert await Book.objects.update_or_create(id=vol2.id, genre='Historic')
assert await Book.objects.count() == 1

View File

@ -15,21 +15,16 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100)
genre: str = ormar.String(
max_length=100,
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
genre: str = ormar.String(max_length=100, default='Fiction',
choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure")
await Book.objects.create(
title="War and Peace in Space", author="Tolstoy, Leo", genre="Fantasy"
)
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
# delete accepts kwargs that will be used in filter
# acting in same way as queryset.filter(**kwargs).delete()
await Book.objects.delete(genre="Fantasy") # delete all fantasy books
await Book.objects.delete(genre='Fantasy') # delete all fantasy books
all_books = await Book.objects.all()
assert len(all_books) == 2

View File

@ -36,27 +36,10 @@ class Car(ormar.Model):
# build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create(
manufacturer=toyota,
name="Corolla",
year=2020,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Yaris",
year=2019,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Supreme",
year=2020,
gearbox_type="Auto",
gears=6,
aircon_type="Auto",
)
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
aircon_type='Manual')
await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
aircon_type='Manual')
await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
aircon_type='Auto')

View File

@ -36,65 +36,33 @@ class Car(ormar.Model):
# build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create(
manufacturer=toyota,
name="Corolla",
year=2020,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Yaris",
year=2019,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Supreme",
year=2020,
gearbox_type="Auto",
gears=6,
aircon_type="Auto",
)
await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
aircon_type='Manual')
await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
aircon_type='Manual')
await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
aircon_type='Auto')
# select manufacturer but only name - to include related models use notation {model_name}__{column}
all_cars = (
await Car.objects.select_related("manufacturer")
.exclude_fields(
["year", "gearbox_type", "gears", "aircon_type", "company__founded"]
)
.all()
)
all_cars = await Car.objects.select_related('manufacturer').exclude_fields(
['year', 'gearbox_type', 'gears', 'aircon_type', 'company__founded']).all()
for car in all_cars:
# excluded columns will yield None
assert all(
getattr(car, x) is None
for x in ["year", "gearbox_type", "gears", "aircon_type"]
)
assert all(getattr(car, x) is None for x in ['year', 'gearbox_type', 'gears', 'aircon_type'])
# included column on related models will be available, pk column is always included
# even if you do not include it in fields list
assert car.manufacturer.name == "Toyota"
assert car.manufacturer.name == 'Toyota'
# also in the nested related models - you cannot exclude pk - it's always auto added
assert car.manufacturer.founded is None
# fields() can be called several times, building up the columns to select
# models selected in select_related but with no columns in fields list implies all fields
all_cars = (
await Car.objects.select_related("manufacturer")
.exclude_fields("year")
.exclude_fields(["gear", "gearbox_type"])
.all()
)
all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year').exclude_fields(
['gear', 'gearbox_type']).all()
# all fiels from company model are selected
assert all_cars[0].manufacturer.name == "Toyota"
assert all_cars[0].manufacturer.name == 'Toyota'
assert all_cars[0].manufacturer.founded == 1937
# cannot exclude mandatory model columns - company__name in this example - note usage of dict/set this time
await Car.objects.select_related("manufacturer").exclude_fields(
[{"company": {"name"}}]
).all()
await Car.objects.select_related('manufacturer').exclude_fields([{'company': {'name'}}]).all()
# will raise pydantic ValidationError as company.name is required