Skip to content

Commit 3047d2e

Browse files
committed
Small README update
1 parent 6f600d1 commit 3047d2e

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class MovieSerializer(serializers.ModelSerializer):
6565

6666
```
6767

68-
Note that for proper performance and functionality, all nested serializers must have a corresponding `prefetch_related` on the queryset used by `MovieSerializer`. Also, the `nomination_count` field should be `annotate`d on it. Therefore, you'll need to write this complex chain of nested prefetches:
68+
For good performance and correct functionality, all nested serializers must have a corresponding `prefetch_related` on the queryset used by `MovieSerializer`. Also, the `nomination_count` field should be `annotate`d on it. Therefore, you'll need to write this complex chain of nested prefetches:
6969

7070
```python
7171
from django.db.models import Prefetch
@@ -120,31 +120,42 @@ class VirtualMovie(v.VirtualModel):
120120

121121
class Meta:
122122
model = Movie
123-
124-
qs = VirtualMovie().get_optimized_queryset(
125-
Movie.objects.all(),
126-
lookup_list=[
127-
"directors__awards",
128-
"directors__nomination_count",
129-
]
130-
)
131123
```
132124

133-
If, for example, you forget to add the `nomination_count` field on `VirtualPerson`, the following exception will appear when using `MovieSerializer`:
134-
135-
![MissingVirtualModelFieldException exception](https://user-images.githubusercontent.com/397989/193944879-5205d80b-4102-415e-b178-7630a14db5a1.png)
136-
137-
To configure your view and serializer to use Virtual Models, inherit from the proper classes:
125+
To configure your DRF view and serializer to use Virtual Models, inherit from the proper classes:
138126

139127
```python
140128
import django_virtual_models as v
141129

142130
class MovieSerializer(v.VirtualModelSerializer):
143131
...
144132

133+
class Meta:
134+
...
135+
virtual_model = VirtualMovie
136+
145137
class MovieList(v.VirtualModelListAPIView):
146138
queryset = Movie.objects.all()
147139
serializer_class = MovieSerializer
140+
...
141+
```
142+
143+
**Then the library will automatically do the right prefetches and annotations for you!**
144+
145+
If, for example, you forget to add the `nomination_count` field on `VirtualPerson`, the following exception will appear when using `MovieSerializer`:
146+
147+
![MissingVirtualModelFieldException exception](https://user-images.githubusercontent.com/397989/193944879-5205d80b-4102-415e-b178-7630a14db5a1.png)
148+
149+
If you aren't using DRF serializers, you hydrate your queryset with *virtual fields* manually:
150+
151+
```python
152+
qs = VirtualMovie().get_optimized_queryset(
153+
Movie.objects.all(),
154+
lookup_list=[
155+
"directors__awards",
156+
"directors__nomination_count",
157+
]
158+
)
148159
```
149160

150161
To learn more, check the [Installation](https://vintasoftware.github.io/django-virtual-models/installation/) and the [Tutorial](https://vintasoftware.github.io/django-virtual-models/tutorial/).

0 commit comments

Comments
 (0)