Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e96fe6c

Browse files
committed
Adding select_related and prefetch_for_includes to CommentViewSet
1 parent 5a7d73a commit e96fe6c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

‎example/tests/test_performance.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ def test_query_count_include_author(self):
4848
4949
1. Primary resource COUNT query
5050
2. Primary resource SELECT
51-
3. Author's prefetched
51+
3. Authors prefetched
52+
3. Entries prefetched
5253
"""
53-
with self.assertNumQueries(3):
54+
with self.assertNumQueries(4):
5455
response = self.client.get('/comments?include=author&page_size=25')
5556
self.assertEqual(len(response.data['results']), 25)

‎example/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,13 @@ class AuthorViewSet(ModelViewSet):
7373

7474

7575
class CommentViewSet(ModelViewSet):
76-
queryset = Comment.objects.all()
76+
queryset = Comment.objects.select_related('author', 'entry')
7777
serializer_class = CommentSerializer
78+
prefetch_for_includes = {
79+
'__all__': [],
80+
'author': ['author', 'author__bio', 'author__entries'],
81+
'entry': ['author', 'author__bio', 'author__entries']
82+
}
7883

7984

8085
class CompanyViewset(ModelViewSet):

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /