[Fixed]-Serialize DynamicField

1👍

You can do that with python eval’s method and DRF SerializerMethodField.

Try this.

from rest_framework import serializers as drf_serailizer

class SettingsSerializer(serializers.DocumentSerializer):
    range = drf_serailizer.SerializerMethodField()

    class Meta:
        model = Settings
        fields = [
            'name',
            'range'
        ]

    def get_range(self, object):
        try:
            return eval(object.range)
        except:
            return None

Leave a comment