You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorial.md
+16-16Lines changed: 16 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -435,54 +435,54 @@ class PersonSerializer(v.VirtualModelSerializer):
435
435
436
436
This serializer needs the `awards` field from `VirtualPerson` inside `get_has_won_any_award` method, otherwise it would raise an `AttributeError` as this is a *virtual field*. But how to specify that requirement? There are multiple ways of doing this:
437
437
438
-
#### Using type hints with `prefetch.Required`
438
+
#### Using type hints with `hints.Virtual`
439
439
440
440
This library supports special type hints for informing what virtual fields a serializer method needs:
441
441
442
442
```python
443
443
# Python < 3.9. For >= 3.9, use `from typing import Annotated`:
Now thanks to `prefetch.Required("awards")`, this serializer will be able to require the field `awards` from the virtual model. If necessary, you can pass multiple fields or even nested lookups: `prefetch.Required("awards", "nominations__movie")`.
458
+
Now thanks to `hints.Virtual("awards")`, this serializer will be able to require the field `awards` from the virtual model. If necessary, you can pass multiple fields or even nested lookups: `hints.Virtual("awards", "nominations__movie")`.
459
459
460
460
!!! note
461
-
`prefetch.Required` can be used to ensure the fetching of any `deferred_fields` a method needs without N+1s.
461
+
`hints.Virtual` can be used to ensure the fetching of any `deferred_fields` a method needs without N+1s.
462
462
463
463
!!! note
464
464
[Annotated](https://docs.python.org/3/library/typing.html#typing.Annotated) is a built-in Python construct that allows us to add custom annotations that don't affect type checking.
465
465
466
466
#### Using `from_types_of` decorator
467
467
468
-
If your method calls another function, you can add type hints to that function with `prefetch.Required` and then use the `from_types_of` decorator:
468
+
If your method calls another function, you can add type hints to that function with `hints.Virtual` and then use the `from_types_of` decorator:
469
469
470
470
```python
471
471
# Python < 3.9. For >= 3.9, use `from typing import Annotated`:
@@ -494,12 +494,12 @@ Note the function `check_person_has_won_any_award` is received in the decorator
494
494
If you have the `has_won_any_award` field in your virtual model, you can use the `defined_on_virtual_model` decorator:
495
495
496
496
```python
497
-
from django_virtual_models importprefetch
497
+
from django_virtual_models importhints
498
498
499
499
classPersonSerializer(v.VirtualModelSerializer):
500
500
...
501
501
502
-
@prefetch.hints.defined_on_virtual_model() # <--- here
502
+
@hints.defined_on_virtual_model() # <--- here
503
503
defget_has_won_any_award(self, person):
504
504
return person.has_won_any_award
505
505
```
@@ -509,7 +509,7 @@ class PersonSerializer(v.VirtualModelSerializer):
509
509
If your method field only uses concrete fields, in other words, it doesn't depend on any virtual model fields, nor any `deferred_fields`, you can use the `no_deferred_fields` decorator:
@@ -519,7 +519,7 @@ class PersonSerializer(v.VirtualModelSerializer):
519
519
virtual_model = VirtualPerson
520
520
fields = ["name_title_case"]
521
521
522
-
@prefetch.hints.no_deferred_fields() # <--- here
522
+
@hints.no_deferred_fields() # <--- here
523
523
defget_name_title_case(self, person):
524
524
return person.name.title()
525
525
```
@@ -529,18 +529,18 @@ class PersonSerializer(v.VirtualModelSerializer):
529
529
DRF Serializers can use directly model properties and methods in `Meta.fields`. If your serializer uses a model property or method, you need to use the same hints described by the previous section ["Prefetching for method fields"](#prefetching-for-method-fields-serializermethodfield). For example:
0 commit comments