from rest_framework import serializers from perms.models import Action __all__ = ['ActionsDisplayField', 'ActionsField'] class ActionsField(serializers.MultipleChoiceField): def __init__(self, *args, **kwargs): kwargs['choices'] = Action.CHOICES super().__init__(*args, **kwargs) def to_representation(self, value): return Action.value_to_choices(value) def to_internal_value(self, data): if data is None: return data return Action.choices_to_value(data) class ActionsDisplayField(ActionsField): def to_representation(self, value): values = super().to_representation(value) choices = dict(Action.CHOICES) return [choices.get(i) for i in values]