-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix UpdateModelMixin to work when no queryset is defined is defined on the view #9314
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,13 +69,14 @@ def update(self, request, *args, **kwargs): | |
serializer.is_valid(raise_exception=True) | ||
self.perform_update(serializer) | ||
|
||
queryset = self.filter_queryset(self.get_queryset()) | ||
if queryset._prefetch_related_lookups: | ||
if hasattr(instance, '_prefetched_objects_cache'): | ||
# If 'prefetch_related' has been applied to a queryset, we need to | ||
# forcibly invalidate the prefetch cache on the instance, | ||
# and then re-prefetch related objects | ||
# forcibly invalidate the prefetch cache on the instance | ||
instance._prefetched_objects_cache = {} | ||
prefetch_related_objects([instance], *queryset._prefetch_related_lookups) | ||
queryset = self.filter_queryset(self.get_queryset()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still expecting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is it possible that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I meant by my comment earlier, one might do: class UserRetrieveWithoutQuerySet(generics.RetrieveUpdateAPIView):
serializer_class = UserSerializer
def get_object(self):
return User.objects.prefetch_related('groups').get(pk=self.kwargs['pk']) In such case, I would argue that it's reasonable to require users to override the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably just an extra |
||
if getattr(queryset, '_prefetch_related_lookups', None): | ||
# And then re-prefetch related objects | ||
prefetch_related_objects([instance], *queryset._prefetch_related_lookups) | ||
|
||
return Response(serializer.data) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When only
get_object
is defined, I assume that folks are unlikely to have advanced prefetches in their method (although it's very much possible).If some things were prefetched, we assume that there is a
get_queryset
method.