from functools import lru_cache

from rest_framework import serializers


class AbstractFieldSerializer(serializers.RelatedField):
    parent_serializer_class = serializers.ModelSerializer

    def to_representation(self, image):
        return self.parent_serializer_class(image, context=self.context).data


class AbstractSerializerMixin(object):

    @classmethod
    @lru_cache()
    def get_field_serializer_class(cls):
        return type('%sFieldSerializer' % cls.Meta.model, (AbstractFieldSerializer,), {
            "parent_serializer_class": cls
        })

    @classmethod
    def get_field_serializer(cls, **kwargs):
        return cls.get_field_serializer_class()(**kwargs)

from functools import lru_cache

from rest_framework import serializers
from rest_framework.relations import ManyRelatedField, MANY_RELATION_KWARGS


class AbstractFieldSerializer(serializers.RelatedField):
    parent_serializer_class = serializers.ModelSerializer
    many_field_class = ManyRelatedField

    def to_representation(self, image):
        return self.parent_serializer_class(image, context=self.context).data

    @classmethod
    def many_init(cls, *args, **kwargs):
        list_kwargs = {'child_relation': cls(*args, **kwargs)}
        for key in kwargs.keys():
            if key in MANY_RELATION_KWARGS:
                list_kwargs[key] = kwargs[key]
        return cls.many_field_class(**list_kwargs)


class AbstractSerializerMixin(object):

    @classmethod
    @lru_cache()
    def get_field_serializer_class(cls, many_field_class=ManyRelatedField):
        return type('%sFieldSerializer' % cls.Meta.model, (AbstractFieldSerializer,), {
            "parent_serializer_class": cls,
            "many_field_class": many_field_class,
        })

    @classmethod
    def get_field_serializer(cls, **kwargs):
        many_field_class = kwargs.pop("many_field_class", ManyRelatedField)
        return cls.get_field_serializer_class(many_field_class=many_field_class)(**kwargs)

Allow overriding the ManyRelatedField for image fields
Allow overriding the ManyRelatedField for image fields

  from functools import lru_cache
  
  from rest_framework import serializers
+ from rest_framework.relations import ManyRelatedField, MANY_RELATION_KWARGS
  
  
  class AbstractFieldSerializer(serializers.RelatedField):
      parent_serializer_class = serializers.ModelSerializer
+     many_field_class = ManyRelatedField
  
      def to_representation(self, image):
          return self.parent_serializer_class(image, context=self.context).data
+ 
+     @classmethod
+     def many_init(cls, *args, **kwargs):
+         list_kwargs = {'child_relation': cls(*args, **kwargs)}
+         for key in kwargs.keys():
+             if key in MANY_RELATION_KWARGS:
+                 list_kwargs[key] = kwargs[key]
+         return cls.many_field_class(**list_kwargs)
  
  
  class AbstractSerializerMixin(object):
  
      @classmethod
      @lru_cache()
-     def get_field_serializer_class(cls):
+     def get_field_serializer_class(cls, many_field_class=ManyRelatedField):
          return type('%sFieldSerializer' % cls.Meta.model, (AbstractFieldSerializer,), {
-             "parent_serializer_class": cls
+             "parent_serializer_class": cls,
+             "many_field_class": many_field_class,
          })
  
      @classmethod
      def get_field_serializer(cls, **kwargs):
+         many_field_class = kwargs.pop("many_field_class", ManyRelatedField)
-         return cls.get_field_serializer_class()(**kwargs)
+         return cls.get_field_serializer_class(many_field_class=many_field_class)(**kwargs)
  
Allow overriding the ManyRelatedField for image fields
## Code Before:
from functools import lru_cache

from rest_framework import serializers


class AbstractFieldSerializer(serializers.RelatedField):
    parent_serializer_class = serializers.ModelSerializer

    def to_representation(self, image):
        return self.parent_serializer_class(image, context=self.context).data


class AbstractSerializerMixin(object):

    @classmethod
    @lru_cache()
    def get_field_serializer_class(cls):
        return type('%sFieldSerializer' % cls.Meta.model, (AbstractFieldSerializer,), {
            "parent_serializer_class": cls
        })

    @classmethod
    def get_field_serializer(cls, **kwargs):
        return cls.get_field_serializer_class()(**kwargs)

## Instruction:
Allow overriding the ManyRelatedField for image fields
## Code After:
from functools import lru_cache

from rest_framework import serializers
from rest_framework.relations import ManyRelatedField, MANY_RELATION_KWARGS


class AbstractFieldSerializer(serializers.RelatedField):
    parent_serializer_class = serializers.ModelSerializer
    many_field_class = ManyRelatedField

    def to_representation(self, image):
        return self.parent_serializer_class(image, context=self.context).data

    @classmethod
    def many_init(cls, *args, **kwargs):
        list_kwargs = {'child_relation': cls(*args, **kwargs)}
        for key in kwargs.keys():
            if key in MANY_RELATION_KWARGS:
                list_kwargs[key] = kwargs[key]
        return cls.many_field_class(**list_kwargs)


class AbstractSerializerMixin(object):

    @classmethod
    @lru_cache()
    def get_field_serializer_class(cls, many_field_class=ManyRelatedField):
        return type('%sFieldSerializer' % cls.Meta.model, (AbstractFieldSerializer,), {
            "parent_serializer_class": cls,
            "many_field_class": many_field_class,
        })

    @classmethod
    def get_field_serializer(cls, **kwargs):
        many_field_class = kwargs.pop("many_field_class", ManyRelatedField)
        return cls.get_field_serializer_class(many_field_class=many_field_class)(**kwargs)
