-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit 2b34aa4.
- Loading branch information
Showing
2 changed files
with
32 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
from django.contrib.auth.models import Group, User | ||
from django.db.models.query import Prefetch | ||
from django.test import TestCase | ||
|
||
from rest_framework import generics, serializers | ||
|
@@ -9,84 +8,51 @@ | |
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
permissions = serializers.SerializerMethodField() | ||
|
||
def get_permissions(self, obj): | ||
ret = [] | ||
for g in obj.groups.all(): | ||
ret.extend([p.pk for p in g.permissions.all()]) | ||
return ret | ||
|
||
class Meta: | ||
model = User | ||
fields = ('id', 'username', 'email', 'groups', 'permissions') | ||
|
||
|
||
class UserRetrieveUpdate(generics.RetrieveUpdateAPIView): | ||
queryset = User.objects.exclude(username='exclude').prefetch_related( | ||
Prefetch('groups', queryset=Group.objects.exclude(name='exclude')), | ||
'groups__permissions', | ||
) | ||
serializer_class = UserSerializer | ||
fields = ('id', 'username', 'email', 'groups') | ||
|
||
|
||
class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView): | ||
queryset = User.objects.exclude(username='exclude') | ||
class UserUpdate(generics.UpdateAPIView): | ||
queryset = User.objects.exclude(username='exclude').prefetch_related('groups') | ||
serializer_class = UserSerializer | ||
|
||
|
||
class TestPrefetchRelatedUpdates(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create(username='tom', email='[email protected]') | ||
self.groups = [Group.objects.create(name=f'group {i}') for i in range(10)] | ||
self.groups = [Group.objects.create(name='a'), Group.objects.create(name='b')] | ||
self.user.groups.set(self.groups) | ||
self.user.groups.add(Group.objects.create(name='exclude')) | ||
self.expected = { | ||
'id': self.user.pk, | ||
'username': 'tom', | ||
'groups': [group.pk for group in self.groups], | ||
'email': '[email protected]', | ||
'permissions': [], | ||
} | ||
self.view = UserRetrieveUpdate.as_view() | ||
|
||
def test_prefetch_related_updates(self): | ||
self.groups.append(Group.objects.create(name='c')) | ||
request = factory.put( | ||
'/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json' | ||
) | ||
self.expected['username'] = 'new' | ||
self.expected['groups'] = [group.pk for group in self.groups] | ||
response = self.view(request, pk=self.user.pk) | ||
assert User.objects.get(pk=self.user.pk).groups.count() == 12 | ||
assert response.data == self.expected | ||
# Update and fetch should get same result | ||
request = factory.get('/') | ||
response = self.view(request, pk=self.user.pk) | ||
assert response.data == self.expected | ||
view = UserUpdate.as_view() | ||
pk = self.user.pk | ||
groups_pk = self.groups[0].pk | ||
request = factory.put('/', {'username': 'new', 'groups': [groups_pk]}, format='json') | ||
response = view(request, pk=pk) | ||
assert User.objects.get(pk=pk).groups.count() == 1 | ||
expected = { | ||
'id': pk, | ||
'username': 'new', | ||
'groups': [1], | ||
'email': '[email protected]' | ||
} | ||
assert response.data == expected | ||
|
||
def test_prefetch_related_excluding_instance_from_original_queryset(self): | ||
""" | ||
Regression test for https://github.com/encode/django-rest-framework/issues/4661 | ||
""" | ||
request = factory.put( | ||
'/', {'username': 'exclude', 'groups': [self.groups[0].pk]}, format='json' | ||
) | ||
response = self.view(request, pk=self.user.pk) | ||
assert User.objects.get(pk=self.user.pk).groups.count() == 2 | ||
self.expected['username'] = 'exclude' | ||
self.expected['groups'] = [self.groups[0].pk] | ||
assert response.data == self.expected | ||
|
||
def test_db_query_count(self): | ||
request = factory.put( | ||
'/', {'username': 'new'}, format='json' | ||
) | ||
with self.assertNumQueries(7): | ||
self.view(request, pk=self.user.pk) | ||
|
||
request = factory.put( | ||
'/', {'username': 'new2'}, format='json' | ||
) | ||
with self.assertNumQueries(16): | ||
UserUpdateWithoutPrefetchRelated.as_view()(request, pk=self.user.pk) | ||
view = UserUpdate.as_view() | ||
pk = self.user.pk | ||
groups_pk = self.groups[0].pk | ||
request = factory.put('/', {'username': 'exclude', 'groups': [groups_pk]}, format='json') | ||
response = view(request, pk=pk) | ||
assert User.objects.get(pk=pk).groups.count() == 1 | ||
expected = { | ||
'id': pk, | ||
'username': 'exclude', | ||
'groups': [1], | ||
'email': '[email protected]' | ||
} | ||
assert response.data == expected |