          self.groups = [Group.objects.create(name='a'), Group.objects.create(name='b')]
          self.user.groups.set(self.groups)
  
      def test_prefetch_related_updates(self):
          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
  
      def test_prefetch_related_excluding_instance_from_original_queryset(self):
          """
          Regression test for https://github.com/encode/django-rest-framework/issues/4661
          """
 
</s> remove         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
 </s> add         request = factory.put(
            '/', {'username': 'exclude', 'groups': [self.groups[0].pk]}, format='json'
        )
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 2
        self.expected['username'] = 'exclude'
        self.expected['groups'] = [self.groups[0].pk]
        assert response.data == self.expected

    def test_db_query_count(self):
        request = factory.put(
            '/', {'username': 'new'}, format='json'
        )
        with self.assertNumQueries(7):
            self.view(request, pk=self.user.pk)

        request = factory.put(
            '/', {'username': 'new2'}, format='json'
        )
        with self.assertNumQueries(16):
            UserUpdateWithoutPrefetchRelated.as_view()(request, pk=self.user.pk) </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 class UserUpdate(generics.UpdateAPIView):
    queryset = User.objects.exclude(username='exclude').prefetch_related('groups')
 </s> add class UserUpdateWithoutPrefetchRelated(generics.UpdateAPIView):
    queryset = User.objects.exclude(username='exclude')
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
          self.groups = [Group.objects.create(name='a'), Group.objects.create(name='b')]
          self.user.groups.set(self.groups)
  
      def test_prefetch_related_updates(self):
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
        self.groups.append(Group.objects.create(name='c'))
        request = factory.put(
            '/', {'username': 'new', 'groups': [group.pk for group in self.groups]}, format='json'
        )
        self.expected['username'] = 'new'
        self.expected['groups'] = [group.pk for group in self.groups]
        response = self.view(request, pk=self.user.pk)
        assert User.objects.get(pk=self.user.pk).groups.count() == 12
        assert response.data == self.expected
        # Update and fetch should get same result
        request = factory.get('/')
        response = self.view(request, pk=self.user.pk)
        assert response.data == self.expected
  
      def test_prefetch_related_excluding_instance_from_original_queryset(self):
          """
          Regression test for https://github.com/encode/django-rest-framework/issues/4661
          """
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace 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