from rest_framework.views import APIView
from rest_framework.response import Response


class EnumView(APIView):
    permission_classes = []

    def get(self, *args, **kwargs):
        enums = self.enum_class.get_as_tuple_list()

        context = []
        for enum in enums:
            _id = enum[1]
            i18n = self.enum_class.i18n[_id]

            context.append({
                'id': _id,
                'i18n': i18n,
            })

        return Response(context)

from rest_framework.views import APIView
from rest_framework.response import Response


class EnumView(APIView):
    permission_classes = []
    fields = ('i18n', )

    def get(self, *args, **kwargs):
        enums = self.enum_class.get_as_tuple_list()
        context = []

        for enum in enums:
            _id = enum[1]
            enum_context = {'id': _id}

            for field in self.fields:
                enum_context[field] = getattr(self.enum_class, field)[_id]

            context.append(enum_context)

        return Response(context)

Add possibility to set fields
Add possibility to set fields
  from rest_framework.views import APIView
  from rest_framework.response import Response
  
  
  class EnumView(APIView):
      permission_classes = []
+     fields = ('i18n', )
  
      def get(self, *args, **kwargs):
          enums = self.enum_class.get_as_tuple_list()
+         context = []
  
-         context = []
          for enum in enums:
              _id = enum[1]
-             i18n = self.enum_class.i18n[_id]
+             enum_context = {'id': _id}
  
+             for field in self.fields:
+                 enum_context[field] = getattr(self.enum_class, field)[_id]
+ 
-             context.append({
+             context.append(enum_context)
-                 'id': _id,
-                 'i18n': i18n,
-             })
  
          return Response(context)
  
Add possibility to set fields
## Code Before:
from rest_framework.views import APIView
from rest_framework.response import Response


class EnumView(APIView):
    permission_classes = []

    def get(self, *args, **kwargs):
        enums = self.enum_class.get_as_tuple_list()

        context = []
        for enum in enums:
            _id = enum[1]
            i18n = self.enum_class.i18n[_id]

            context.append({
                'id': _id,
                'i18n': i18n,
            })

        return Response(context)

## Instruction:
Add possibility to set fields
## Code After:
from rest_framework.views import APIView
from rest_framework.response import Response


class EnumView(APIView):
    permission_classes = []
    fields = ('i18n', )

    def get(self, *args, **kwargs):
        enums = self.enum_class.get_as_tuple_list()
        context = []

        for enum in enums:
            _id = enum[1]
            enum_context = {'id': _id}

            for field in self.fields:
                enum_context[field] = getattr(self.enum_class, field)[_id]

            context.append(enum_context)

        return Response(context)
