          model = User
          fields = ('id', 'username', 'email', 'groups')
  
  
  class UserUpdate(generics.UpdateAPIView):
      queryset = User.objects.exclude(username='exclude').prefetch_related('groups')
      serializer_class = UserSerializer
  
  
  class TestPrefetchRelatedUpdates(TestCase):
      def setUp(self):
 
</s> remove         fields = ('id', 'username', 'email', 'groups')
 </s> add         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 </s> remove         self.groups = [Group.objects.create(name='a'), Group.objects.create(name='b')]
 </s> add         self.groups = [Group.objects.create(name=f'group {i}') for i in range(10)] </s> remove         if getattr(instance, '_prefetched_objects_cache', None):
 </s> add         queryset = self.filter_queryset(self.get_queryset())
        if queryset._prefetch_related_lookups:
class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView):
    queryset = User.objects.exclude(username='exclude')
          model = User
          fields = ('id', 'username', 'email', 'groups')
  
  
class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView):
    queryset = User.objects.exclude(username='exclude')
class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView):
    queryset = User.objects.exclude(username='exclude')
      serializer_class = UserSerializer
  
  
  class TestPrefetchRelatedUpdates(TestCase):
      def setUp(self):
keep keep keep keep replace replace keep keep keep keep keep
 Re-prefetch related objects after updating (#8043)

* Re-prefetch related objects after updating

* Fix flake8 format

* Use _prefetch_related_lookups and refine test cases

* Add more test cases and refine prefetch checking 
tests/test_prefetch_related.py