Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept subset of the Enum values #89

Open
ADR-007 opened this issue Oct 31, 2018 · 2 comments
Open

Accept subset of the Enum values #89

ADR-007 opened this issue Oct 31, 2018 · 2 comments

Comments

@ADR-007
Copy link

ADR-007 commented Oct 31, 2018

Hi there,

I propose to change this
kwargs['choices'] = tuple((e.value, getattr(e, 'label', e.name)) for e in self.enum)
to this
kwargs['choices'] = tuple((e.value, getattr(e, 'label', e.name)) for e in kwargs.get('choices', self.enum))
to have posibility accept only part of the enum.

@akx
Copy link
Contributor

akx commented Jan 16, 2020

This is a good idea, but we must then make sure the user's choices are in fact valid values for the enum.

@MehdiDRISSIB
Copy link

MehdiDRISSIB commented Sep 19, 2021

Hello,

Yes It could be really useful to subset Enum values by adding a parameter on the field for exemple in serializer. Something like that:

class GatewayName(Enum):
    PAYPAL = 'Paypal'
    STRIPE = 'Stripe'
    WESTERN_UNION = 'Western Union'
    WALLET = "Wallet"
    OTHER = "Other"

    class Labels:
        PAYPAL = _('Paypal')
        STRIPE = _('Stripe')
        WESTERN_UNION = _('Western Union')
        WALLET = _('Wallet')
        OTHER = _("Other")

class DepositSerializer(EnumSupportSerializerMixin, serializers.ModelSerializer):
    gateway = EnumField(GatewayName, required=True, choices=(GatewayName.PAYPAL, GatewayName.STRIPE))

But now choices are not taking in account and the serializer accept all enum values.
Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants