2020-06-26 10:57:11 -05:00
|
|
|
from rest_framework.metadata import SimpleMetadata
|
|
|
|
from django.utils.encoding import force_str
|
|
|
|
from utilities.api import ContentTypeField
|
|
|
|
|
|
|
|
|
2020-06-26 11:09:27 -05:00
|
|
|
class ContentTypeMetadata(SimpleMetadata):
|
2020-06-26 10:57:11 -05:00
|
|
|
|
|
|
|
def get_field_info(self, field):
|
|
|
|
field_info = super().get_field_info(field)
|
|
|
|
if hasattr(field, 'queryset') and not field_info.get('read_only') and isinstance(field, ContentTypeField):
|
|
|
|
field_info['choices'] = [
|
|
|
|
{
|
|
|
|
'value': choice_value,
|
|
|
|
'display_name': force_str(choice_name, strings_only=True)
|
|
|
|
}
|
|
|
|
for choice_value, choice_name in field.choices.items()
|
|
|
|
]
|
2020-06-26 18:42:08 +02:00
|
|
|
field_info['choices'].sort(key=lambda item: item['display_name'])
|
2020-06-26 10:57:11 -05:00
|
|
|
return field_info
|