I need to convert a model selection tuple, like this one from Django docs:
YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
)
Into a JSON object, for example:
[{"id": 'FR', "year": 'Freshman'},
{"id": 'SO', "year": 'Sophomore'},
{"id": 'JR', "year": 'Junior'},
{"id": 'SR', "year": 'Senior'}]
This is a fairly simple conversion and is very often used when using forms, but I could not find any automatic function in Django that works out of the box. I think something is missing here.
source
share