old_contents: from django.contrib.auth import authenticate
  from rest_framework import serializers
  
  from rest_framework_jwt.settings import api_settings
  
  
  jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  
  
  class JSONWebTokenSerializer(serializers.Serializer):
      """
      Serializer class used to validate a username and password.
  
      Returns a JSON Web Token that can be used to authenticate later calls.
      """
      username = serializers.CharField()
      password = serializers.CharField(write_only=True)
  
      def validate(self, attrs):
          username = attrs.get('username')
          password = attrs.get('password')
  
          if username and password:
              user = authenticate(username=username, password=password)
  
              if user:
                  if not user.is_active:
                      msg = 'User account is disabled.'
                      raise serializers.ValidationError(msg)
  
                  payload = jwt_payload_handler(user)
  
                  return {
                      'token': jwt_encode_handler(payload)
                  }
              else:
                  msg = 'Unable to login with provided credentials.'
                  raise serializers.ValidationError(msg)
          else:
              msg = 'Must include "username" and "password"'
              raise serializers.ValidationError(msg)
  
new_contents: from django.contrib.auth import authenticate, get_user_model
  from rest_framework import serializers
  
  from rest_framework_jwt.settings import api_settings
  
  
  jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  
  
  class JSONWebTokenSerializer(serializers.Serializer):
      """
      Serializer class used to validate a username and password.
  
      'username' is identified by the custom UserModel.USERNAME_FIELD.
  
      Returns a JSON Web Token that can be used to authenticate later calls.
      """
  
      username_field = get_user_model().USERNAME_FIELD
      password = serializers.CharField(write_only=True)
  
      def __init__(self, *args, **kwargs):
          """Dynamically add the USERNAME_FIELD to self.fields."""
          super(JSONWebTokenSerializer, self).__init__(*args, **kwargs)
          self.fields[self.username_field] = serializers.CharField()
  
      def validate(self, attrs):
          credentials = {self.username_field: attrs.get(self.username_field),
                         'password': attrs.get('password')}
          if all(credentials.values()):
              user = authenticate(**credentials)
  
              if user:
                  if not user.is_active:
                      msg = 'User account is disabled.'
                      raise serializers.ValidationError(msg)
  
                  payload = jwt_payload_handler(user)
  
                  return {
                      'token': jwt_encode_handler(payload)
                  }
              else:
                  msg = 'Unable to login with provided credentials.'
                  raise serializers.ValidationError(msg)
          else:
              msg = 'Must include "username" and "password"'
              raise serializers.ValidationError(msg)
  
subject: Support Custom User models.