2016-10-11 13:07:40 +03:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from rest_framework import generics, serializers
|
|
|
|
from rest_framework.test import APIRequestFactory
|
|
|
|
|
|
|
|
factory = APIRequestFactory()
|
|
|
|
|
|
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = User
|
2024-03-22 01:23:30 +03:00
|
|
|
fields = ('id', 'username', 'email', 'groups')
|
2016-10-11 13:07:40 +03:00
|
|
|
|
|
|
|
|
2024-03-22 01:23:30 +03:00
|
|
|
class UserUpdate(generics.UpdateAPIView):
|
|
|
|
queryset = User.objects.exclude(username='exclude').prefetch_related('groups')
|
2016-10-11 13:07:40 +03:00
|
|
|
serializer_class = UserSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class TestPrefetchRelatedUpdates(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create(username='tom', email='tom@example.com')
|
2024-03-22 01:23:30 +03:00
|
|
|
self.groups = [Group.objects.create(name='a'), Group.objects.create(name='b')]
|
2017-10-05 21:41:38 +03:00
|
|
|
self.user.groups.set(self.groups)
|
2016-10-11 13:07:40 +03:00
|
|
|
|
|
|
|
def test_prefetch_related_updates(self):
|
2024-03-22 01:23:30 +03:00
|
|
|
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': 'tom@example.com'
|
|
|
|
}
|
|
|
|
assert response.data == expected
|
2016-11-11 12:44:35 +03:00
|
|
|
|
|
|
|
def test_prefetch_related_excluding_instance_from_original_queryset(self):
|
|
|
|
"""
|
2017-04-07 17:28:35 +03:00
|
|
|
Regression test for https://github.com/encode/django-rest-framework/issues/4661
|
2016-11-11 12:44:35 +03:00
|
|
|
"""
|
2024-03-22 01:23:30 +03:00
|
|
|
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': 'tom@example.com'
|
|
|
|
}
|
|
|
|
assert response.data == expected
|
2024-09-11 12:37:33 +03:00
|
|
|
|
|
|
|
def test_can_update_without_queryset_on_class_view(self):
|
|
|
|
class UserUpdateWithoutQuerySet(generics.UpdateAPIView):
|
|
|
|
serializer_class = UserSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
return User.objects.get(pk=self.kwargs['pk'])
|
|
|
|
|
|
|
|
request = factory.patch('/', {'username': 'new'})
|
|
|
|
response = UserUpdateWithoutQuerySet.as_view()(request, pk=self.user.pk)
|
|
|
|
assert response.data['id'] == self.user.id
|
|
|
|
assert response.data['username'] == 'new'
|
|
|
|
self.user.refresh_from_db()
|
|
|
|
assert self.user.username == 'new'
|