Initializing variables in init when using Django Rest Framework for list serializers
<p>I would like to have a serializer where one of the fields in the serializer depends on a value created in the init method.</p>

<p>I have tried to do something like the following:</p>

<pre><code>class MySerializer(serializers.ModelSerializer):
   the_field_value = serializers.SerializerMethodField()
   another_field_value = serializers.SerializerMethodField()

class Meta:
    model = MyModel
    fields = ('id', 'the_field_value', 'another_field_value')

def get_the_field_value(self, obj):
    return "{0} ({1}) {2}".format(obj.name, obj.other_name, self.random_id)

def get_another_field_value(self, obj):
    return "{0} ({1}) {2}".format(obj.other_other_name, obj.other_name, self.random_id)

def __init__(self, *args, **kwargs):
    super(MySerializer, self).__init__(*args, **kwargs)
    self.random_id = randint(1, 5)
</code></pre>

<p>The issue is when using a list serializer the random_id is the same for every list item.</p>

<p>How can I specify an init method to be run for each list item?</p>

<p>Updated with a more detailed example.</p>