update docs, fix for ellipsis for whole model, some more tests
This commit is contained in:
@ -43,25 +43,3 @@ await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_t
|
||||
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').fields(['id', 'name', 'company__name']).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'])
|
||||
# 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'
|
||||
# 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').fields('id').fields(
|
||||
['name']).all()
|
||||
# all fiels from company model are selected
|
||||
assert all_cars[0].manufacturer.name == 'Toyota'
|
||||
assert all_cars[0].manufacturer.founded == 1937
|
||||
|
||||
# cannot exclude mandatory model columns - company__name in this example
|
||||
await Car.objects.select_related('manufacturer').fields(['id', 'name', 'company__founded']).all()
|
||||
# will raise pydantic ValidationError as company.name is required
|
||||
|
||||
@ -63,6 +63,6 @@ all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year
|
||||
assert all_cars[0].manufacturer.name == 'Toyota'
|
||||
assert all_cars[0].manufacturer.founded == 1937
|
||||
|
||||
# cannot exclude mandatory model columns - company__name in this example
|
||||
await Car.objects.select_related('manufacturer').exclude_fields(['company__name']).all()
|
||||
# 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()
|
||||
# will raise pydantic ValidationError as company.name is required
|
||||
|
||||
33
docs_src/queries/docs009.py
Normal file
33
docs_src/queries/docs009.py
Normal file
@ -0,0 +1,33 @@
|
||||
# 1. like in example above
|
||||
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()
|
||||
|
||||
# 3. to include whole nested model use ellipsis
|
||||
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()
|
||||
|
||||
# 5. of course set can have multiple fields
|
||||
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()
|
||||
Reference in New Issue
Block a user