from rest_framework.fields import ChoiceField


class EnumField(ChoiceField):
    default_error_messages = {
        'invalid': ("No matching enum type.",)
    }

    def __init__(self, **kwargs):
        self.enum_type = kwargs.pop("enum_type")
        kwargs.pop("choices", None)
        super(EnumField, self).__init__(self.enum_type.choices(), **kwargs)

    def to_internal_value(self, data):
        for choice in self.enum_type:
            if choice.name == data or choice.value == data:
                return choice
        self.fail('invalid')

    def to_representation(self, value):
        if not value:
            return None
        return value.name

from django.utils.translation import ugettext_lazy as _
from rest_framework.fields import ChoiceField


class EnumField(ChoiceField):
    default_error_messages = {
        'invalid': _("No matching enum type.")
    }

    def __init__(self, **kwargs):
        self.enum_type = kwargs.pop("enum_type")
        kwargs.pop("choices", None)
        super(EnumField, self).__init__(self.enum_type.choices(), **kwargs)

    def to_internal_value(self, data):
        for choice in self.enum_type:
            if choice.name == data or choice.value == data:
                return choice
        self.fail('invalid')

    def to_representation(self, value):
        if not value:
            return None
        return value.name

Return translation instead of tuple
Return translation instead of tuple

Fixes Attributerror: 'tuple' object has no attribute 'format' when POSTing with no value for an Enum field.
+ from django.utils.translation import ugettext_lazy as _
  from rest_framework.fields import ChoiceField
  
  
  class EnumField(ChoiceField):
      default_error_messages = {
-         'invalid': ("No matching enum type.",)
+         'invalid': _("No matching enum type.")
      }
  
      def __init__(self, **kwargs):
          self.enum_type = kwargs.pop("enum_type")
          kwargs.pop("choices", None)
          super(EnumField, self).__init__(self.enum_type.choices(), **kwargs)
  
      def to_internal_value(self, data):
          for choice in self.enum_type:
              if choice.name == data or choice.value == data:
                  return choice
          self.fail('invalid')
  
      def to_representation(self, value):
          if not value:
              return None
          return value.name
  
Return translation instead of tuple
## Code Before:
from rest_framework.fields import ChoiceField


class EnumField(ChoiceField):
    default_error_messages = {
        'invalid': ("No matching enum type.",)
    }

    def __init__(self, **kwargs):
        self.enum_type = kwargs.pop("enum_type")
        kwargs.pop("choices", None)
        super(EnumField, self).__init__(self.enum_type.choices(), **kwargs)

    def to_internal_value(self, data):
        for choice in self.enum_type:
            if choice.name == data or choice.value == data:
                return choice
        self.fail('invalid')

    def to_representation(self, value):
        if not value:
            return None
        return value.name

## Instruction:
Return translation instead of tuple
## Code After:
from django.utils.translation import ugettext_lazy as _
from rest_framework.fields import ChoiceField


class EnumField(ChoiceField):
    default_error_messages = {
        'invalid': _("No matching enum type.")
    }

    def __init__(self, **kwargs):
        self.enum_type = kwargs.pop("enum_type")
        kwargs.pop("choices", None)
        super(EnumField, self).__init__(self.enum_type.choices(), **kwargs)

    def to_internal_value(self, data):
        for choice in self.enum_type:
            if choice.name == data or choice.value == data:
                return choice
        self.fail('invalid')

    def to_representation(self, value):
        if not value:
            return None
        return value.name
