rc for skip of literal binds
This commit is contained in:
@ -26,7 +26,9 @@ 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")
|
||||
|
||||
@ -8,7 +8,9 @@ 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
|
||||
|
||||
|
||||
@ -16,4 +16,5 @@ class Course(ormar.Model):
|
||||
name: ormar.String(max_length=100)
|
||||
completed: ormar.Boolean(default=False)
|
||||
|
||||
c1 = Course()
|
||||
|
||||
c1 = Course()
|
||||
|
||||
@ -14,4 +14,4 @@ class Course(ormar.Model):
|
||||
|
||||
id = ormar.Integer(primary_key=True)
|
||||
name = ormar.String(max_length=100)
|
||||
completed = ormar.Boolean(default=False)
|
||||
completed = ormar.Boolean(default=False)
|
||||
|
||||
@ -19,4 +19,4 @@ class Course(ormar.Model):
|
||||
|
||||
@property_field
|
||||
def prefixed_name(self):
|
||||
return 'custom_prefix__' + self.name
|
||||
return "custom_prefix__" + self.name
|
||||
|
||||
@ -15,14 +15,17 @@ 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
|
||||
|
||||
@ -15,18 +15,23 @@ 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
|
||||
|
||||
@ -15,16 +15,21 @@ 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
|
||||
|
||||
@ -36,10 +36,27 @@ 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",
|
||||
)
|
||||
|
||||
@ -36,33 +36,65 @@ 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
|
||||
|
||||
@ -1,33 +1,29 @@
|
||||
# 1. like in example above
|
||||
await Car.objects.select_related('manufacturer').fields(['id', 'name', 'manufacturer__name']).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
["id", "name", "manufacturer__name"]
|
||||
).all()
|
||||
|
||||
# 2. to mark a field as required use ellipsis
|
||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
||||
'name': ...,
|
||||
'manufacturer': {
|
||||
'name': ...}
|
||||
}).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
{"id": ..., "name": ..., "manufacturer": {"name": ...}}
|
||||
).all()
|
||||
|
||||
# 3. to include whole nested model use ellipsis
|
||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
||||
'name': ...,
|
||||
'manufacturer': ...
|
||||
}).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
{"id": ..., "name": ..., "manufacturer": ...}
|
||||
).all()
|
||||
|
||||
# 4. to specify fields at last nesting level you can also use set - equivalent to 2. above
|
||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
||||
'name': ...,
|
||||
'manufacturer': {'name'}
|
||||
}).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
{"id": ..., "name": ..., "manufacturer": {"name"}}
|
||||
).all()
|
||||
|
||||
# 5. of course set can have multiple fields
|
||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
||||
'name': ...,
|
||||
'manufacturer': {'name', 'founded'}
|
||||
}).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
{"id": ..., "name": ..., "manufacturer": {"name", "founded"}}
|
||||
).all()
|
||||
|
||||
# 6. you can include all nested fields but it will be equivalent of 3. above which is shorter
|
||||
await Car.objects.select_related('manufacturer').fields({'id': ...,
|
||||
'name': ...,
|
||||
'manufacturer': {'id', 'name', 'founded'}
|
||||
}).all()
|
||||
await Car.objects.select_related("manufacturer").fields(
|
||||
{"id": ..., "name": ..., "manufacturer": {"id", "name", "founded"}}
|
||||
).all()
|
||||
|
||||
@ -14,4 +14,4 @@ class Course(ormar.Model):
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
name: str = ormar.String(max_length=100)
|
||||
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
|
||||
department: Optional[Union[Department, Dict]] = ormar.ForeignKey(Department)
|
||||
|
||||
Reference in New Issue
Block a user