  
  
  class UserSerializer(serializers.ModelSerializer):
      class Meta:
          model = User
          fields = ('id', 'username', 'email', 'groups', 'permissions')
  
  
 
</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 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') </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         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
 </s> add         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
    permissions = serializers.SerializerMethodField()

    def get_permissions(self, obj):
        ret = []
        for g in obj.groups.all():
            ret.extend([p.pk for p in g.permissions.all()])
        return ret

  
  
  class UserSerializer(serializers.ModelSerializer):
    permissions = serializers.SerializerMethodField()

    def get_permissions(self, obj):
        ret = []
        for g in obj.groups.all():
            ret.extend([p.pk for p in g.permissions.all()])
        return ret

      class Meta:
          model = User
          fields = ('id', 'username', 'email', 'groups', 'permissions')
  
  
keep keep add 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