title: How to customize to_internal_value function in a RelatedField class for Django REST Framework?
body: <p>In my code I need to make a custom <code>RelatedField</code> class for multiple <code>ModelSerializer</code> classes. I followed this <a href="https://stackoverflow.com/a/55164757/5752558">answer</a>, it's working great. But I need to make several <code>RelatedField</code> classes for every model I have. Is there any way to only make 1 <code>RelatedField</code> class so it can be used for all related fields in my <code>ModelSerializer</code>?<br>
  Currently I'm doing it like this in <code>serializers.py</code>:  </p>
  
  <pre><code>class ColorRelatedField(serializers.RelatedField):
      def display_value(self, instance):
          return instance
  
      def to_representation(self, value):
          return str(value)
  
      def to_internal_value(self, data):
          return ColorParent.objects.get(name=data)
  
  
  class ProductCollectionRelatedField(serializers.RelatedField):
      def display_value(self, instance):
          return instance
  
      def to_representation(self, value):
          return str(value)
  
      def to_internal_value(self, data):
          return ProductCollection.objects.get(name=data)
  
  
  class ProductSerializer(serializers.ModelSerializer):
      collection = ProductCollectionRelatedField(queryset=ProductCollection.objects.all(), many=False)
      color = ColorRelatedField(queryset=ColorParent.objects.all(), many=False)
  
      class Meta:
          model = Product
          fields = ['id', 'product_id', 'collection', 'color', 'video', 'status']
  </code></pre>
  
  <p>How to make <code>to_internal_value</code> function returns a dynamic model depending on who calls the method:  </p>
  
  <pre><code>class CustomRelatedField(serializers.RelatedField, model):
      def display_value(self, instance):
          return instance
  
      def to_representation(self, value):
          return str(value)
  
      def to_internal_value(self, data):
          return model.objects.get(name=data)
  
  
  class ProductSerializer(serializers.ModelSerializer):
      collection = CustomRelatedField(model=ProductCollection, queryset=ProductCollection.objects.all(), many=False)
      color = CustomRelatedField(model=ColorParent, queryset=ColorParent.objects.all(), many=False)
  
      class Meta:
          model = Product
          fields = ['id', 'product_id', 'collection', 'color', 'video', 'status']
  </code></pre>
  
  <p>When I tried to add <code>model</code> parameter in the <code>CustomRelatedField</code> class the error message I got was:   </p>
  
  <blockquote>
    <p>name <code>model</code> is not defined.</p>
  </blockquote>