Serialize a string without changes in Django Rest Framework?
Question: I'm using Python's json.dumps() to convert an array to a string and then store
it in a Django Model. I'm trying to figure out how I can get Django's REST
framework to ignore this field and send it 'as is' without serializing it a
second time.

For example, if the model looks like this(Both fields are CharFields):

> name = "E:\"
>
> path_with_ids= "[{"name": "E:\", "id": 525}]"

I want the REST framework to ignore 'path_with_ids' when serializing so the
JSON output will look like this:

> { "name": "E:\", "path_with_ids": [ {"name": "E:\", "id": 525} ] }

and not like this:

> { "name": "E:\", "path_with_ids": "[{\"name\": \"E:\\\\\", \"id\": 525}]" }

I've tried to make another serializer class that spits out the input it gets
'as is' without success:

**Serializers.py:**

    
    
    class PathWithIds(serializers.CharField):
    
        def to_representation(self, value):
    
            return value.path_with_ids
    
    class FolderSerializer(serializers.ModelSerializer):
    
    field_to_ignore = PathWithIds(source='path_with_ids')
    
      class Meta:
          model = Folder
          fields = ['id', 'field_to_ignore']
    

Please help!


Answer: I ended up using a wasteful and sickening method of deserializing the array
before serializing it again with the REST framework:

**Serializers.py:**

    
    
    import json
    class PathWithIds(serializers.CharField):
    
        def to_representation(self, value):
    
            x = json.loads(value)
            return x
    
    class FolderSerializer(serializers.ModelSerializer):
    
        array_output = PathWithIds(source='field_to_ignore')
    
          class Meta:
              model = Folder
              fields = ['id', 'array_output']
    

**Output in the rest API:**

> { "name": "E:\", "array_output": [ { "name": "E:\", "id": 525 } ] }

