Movatterモバイル変換


[0]ホーム

URL:


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

Prefetch default included resources#900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
sliverc merged 6 commits intodjango-json-api:masterfromSafaAlfulaij:fix-included-serializers
May 2, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -25,6 +25,7 @@ any parts of the framework not mentioned in the documentation should generally b
OrderViewSet.as_view({'get': 'retrieve_related'}),
name='order-related'),
```
* Ensure default `included_resources` are considered when calculating prefetches.


### Deprecated
Expand Down
18 changes: 16 additions & 2 deletionsexample/tests/test_performance.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
from django.utils import timezone
from rest_framework.test import APITestCase

from example.factories import CommentFactory
from example.factories import CommentFactory, EntryFactory
from example.models import Author, Blog, Comment, Entry


Expand DownExpand Up@@ -36,6 +36,7 @@ def setUp(self):
)
self.comment = Comment.objects.create(entry=self.first_entry)
CommentFactory.create_batch(50)
EntryFactory.create_batch(50)

def test_query_count_no_includes(self):
"""We expect a simple list view to issue only two queries.
Expand All@@ -49,7 +50,7 @@ def test_query_count_no_includes(self):
self.assertEqual(len(response.data["results"]), 25)

def test_query_count_include_author(self):
"""We expect a list view with an include havethree queries:
"""We expect a list view with an include havefive queries:

1. Primary resource COUNT query
2. Primary resource SELECT
Expand All@@ -70,3 +71,16 @@ def test_query_select_related_entry(self):
with self.assertNumQueries(2):
response = self.client.get("/comments?include=writer&page[size]=25")
self.assertEqual(len(response.data["results"]), 25)

def test_query_prefetch_uses_included_resources(self):
"""We expect a list view with `included_resources` to have three queries:

1. Primary resource COUNT query
2. Primary resource SELECT
3. Comments prefetched
"""
with self.assertNumQueries(3):
response = self.client.get(
"/entries?fields[entries]=comments&page[size]=25"
)
self.assertEqual(len(response.data["results"]), 25)
2 changes: 1 addition & 1 deletionrest_framework_json_api/serializers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,7 +137,7 @@ def validate_path(serializer_class, field_path, path):
validate_path(this_included_serializer, new_included_field_path, path)

if request and view:
included_resources = get_included_resources(request)
included_resources = get_included_resources(request, self)
for included_field_name in included_resources:
included_field_path = included_field_name.split(".")
if "related_field" in view.kwargs:
Expand Down
8 changes: 6 additions & 2 deletionsrest_framework_json_api/views.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,7 +63,9 @@ def get_prefetch_related(self, include):
def get_queryset(self, *args, **kwargs):
qs = super(PreloadIncludesMixin, self).get_queryset(*args, **kwargs)

included_resources = get_included_resources(self.request)
included_resources = get_included_resources(
self.request, self.get_serializer_class()
)
for included in included_resources + ["__all__"]:

select_related = self.get_select_related(included)
Expand All@@ -82,7 +84,9 @@ def get_queryset(self, *args, **kwargs):
""" This mixin adds automatic prefetching for OneToOne and ManyToMany fields. """
qs = super(AutoPrefetchMixin, self).get_queryset(*args, **kwargs)

included_resources = get_included_resources(self.request)
included_resources = get_included_resources(
self.request, self.get_serializer_class()
)

for included in included_resources + ["__all__"]:
# If include was not defined, trying to resolve it automatically
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp