[Answer]-I am using forms.ModelMultipleChoiceField in django admin and I am want to show a python list saved in database as text

1👍

You’ll need to create your own custom form field, which subclasses forms.ModelMultipleChoiceField and defines the to_python and prepare_value methods. Something like:

import ast

class StrModelMultipleChoiceField(forms.ModelMultipleChoiceField):
    def to_python(self, value):
         value = ast.literal_eval(value)
         return super(StrModelMultipleChoiceField, self).to_python(value)

    def prepare_value(self, value):
         value = (super(StrModelMultipleChoiceField, self).prepare_value(value)
         return unicode(value)

Leave a comment