|
4 | 4 | We don't bind behaviour to http method handlers yet,
|
5 | 5 | which allows mixin classes to be composed in interesting ways.
|
6 | 6 | """
|
7 |
| -from django.db.models.query import prefetch_related_objects |
| 7 | +from django.db.models.query import prefetch_related_objects, QuerySet |
8 | 8 |
|
9 | 9 | from rest_framework import status
|
10 | 10 | from rest_framework.response import Response
|
@@ -69,13 +69,14 @@ def update(self, request, *args, **kwargs):
|
69 | 69 | serializer.is_valid(raise_exception=True)
|
70 | 70 | self.perform_update(serializer)
|
71 | 71 |
|
72 |
| - queryset = self.filter_queryset(self.get_queryset()) |
73 |
| - if queryset._prefetch_related_lookups: |
| 72 | + if hasattr(instance, '_prefetched_objects_cache'): |
74 | 73 | # If 'prefetch_related' has been applied to a queryset, we need to
|
75 |
| - # forcibly invalidate the prefetch cache on the instance, |
76 |
| - # and then re-prefetch related objects |
| 74 | + # forcibly invalidate the prefetch cache on the instance |
77 | 75 | instance._prefetched_objects_cache = {}
|
78 |
| - prefetch_related_objects([instance], *queryset._prefetch_related_lookups) |
| 76 | + queryset = self.filter_queryset(self.get_queryset()) |
| 77 | + if getattr(queryset, '_prefetch_related_lookups', None): |
| 78 | + # And then re-prefetch related objects |
| 79 | + prefetch_related_objects([instance], *queryset._prefetch_related_lookups) |
79 | 80 |
|
80 | 81 | return Response(serializer.data)
|
81 | 82 |
|
|
0 commit comments