from rest_framework import serializers
from django.contrib.auth.models import User, Group


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('name',)


class UserSerializer(serializers.ModelSerializer):
    groups_complete = GroupSerializer(source='groups', read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'first_name',
                  'last_name', 'groups', 'groups_complete')
        extra_kwargs = {
                        'username': {'required': True},
                        'password': {'required': True}
                        }

from rest_framework import serializers
from django.contrib.auth.models import User, Group


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('name',)


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'first_name',
                  'last_name', 'groups')
        extra_kwargs = {
                        'username': {'required': True},
                        'password': {'required': False}
                        }

Remove nested serializer for groups, make password optional in serializer for user edition
Remove nested serializer for groups, make password optional in serializer for user edition

  from rest_framework import serializers
  from django.contrib.auth.models import User, Group
  
  
  class GroupSerializer(serializers.ModelSerializer):
      class Meta:
          model = Group
          fields = ('name',)
  
  
  class UserSerializer(serializers.ModelSerializer):
-     groups_complete = GroupSerializer(source='groups', read_only=True, many=True)
- 
      class Meta:
          model = User
          fields = ('id', 'username', 'password', 'first_name',
-                   'last_name', 'groups', 'groups_complete')
+                   'last_name', 'groups')
          extra_kwargs = {
                          'username': {'required': True},
-                         'password': {'required': True}
+                         'password': {'required': False}
                          }
  
Remove nested serializer for groups, make password optional in serializer for user edition
## Code Before:
from rest_framework import serializers
from django.contrib.auth.models import User, Group


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('name',)


class UserSerializer(serializers.ModelSerializer):
    groups_complete = GroupSerializer(source='groups', read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'first_name',
                  'last_name', 'groups', 'groups_complete')
        extra_kwargs = {
                        'username': {'required': True},
                        'password': {'required': True}
                        }

## Instruction:
Remove nested serializer for groups, make password optional in serializer for user edition
## Code After:
from rest_framework import serializers
from django.contrib.auth.models import User, Group


class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = ('name',)


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'first_name',
                  'last_name', 'groups')
        extra_kwargs = {
                        'username': {'required': True},
                        'password': {'required': False}
                        }
