forked from encode/django-rest-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix UpdateModelMixin to work when no queryset is defined
Fix encode#9306
- Loading branch information
1 parent
77ef27f
commit 89c79d6
Showing
2 changed files
with
21 additions
and
5 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 |
---|---|---|
|
@@ -35,6 +35,13 @@ class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView): | |
serializer_class = UserSerializer | ||
|
||
|
||
class UserRetrieveWithoutQuerySet(generics.RetrieveUpdateAPIView): | ||
serializer_class = UserSerializer | ||
|
||
def get_object(self): | ||
return User.objects.get(pk=self.kwargs['pk']) | ||
|
||
|
||
class TestPrefetchRelatedUpdates(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create(username='tom', email='[email protected]') | ||
|
@@ -90,3 +97,11 @@ def test_db_query_count(self): | |
) | ||
with self.assertNumQueries(16): | ||
UserUpdateWithoutPrefetchRelated.as_view()(request, pk=self.user.pk) | ||
|
||
def test_can_update_without_queryset(self): | ||
request = factory.patch('/', {'username': 'new'}) | ||
response = UserRetrieveWithoutQuerySet.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' |