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

Enum defined in a parent abstract model class is not transformed as enum #344

Open
AMontagu opened this issue Feb 5, 2025 · 0 comments
Open

Comments

@AMontagu
Copy link
Collaborator

AMontagu commented Feb 5, 2025

class PreferedContactWay(models.TextChoices):
    PHONE_CALL = "PHONE_CALL", _("PHONE_CALL")
    EMAIL = "EMAIL", _("EMAIL")
    PHONE_MESSAGE = "PHONE_MESSAGE", _("PHONE_MESSAGE")
    WHATS_APP = "WHATS_APP", _("WHATS_APP"

Class PreferedContactWay(models.Model):
    prefered_contact_way: Annotated[models.CharField, PreferedContactWay] = models.CharField(
        max_length=253,
        verbose_name=_("Prefered way to contact"),
        null=True,
        blank=True,
        choices=PreferedContactWay,
    )

    class Meta:
        verbose_name = _("Abstract Contact Info")
        verbose_name_plural = _("Abstract Contact Info")
        abstract = True

Class Contact1(PreferedContactWay):
  ...

This code when generating proto do not transform prefered_contact_way field into an enum.

A way around for now is to redefine the field in the serializer like this:

class Contact1ProtoSerializer(proto_serializers.ModelProtoSerializer):
  prefered_contact_way: Annotated[serializers.ChoiceField, PreferedContactWay] = (
        serializers.ChoiceField(choices=PreferedContactWay)
    )

    class Meta:
        model = Contact1
        fields = [
            "prefered_contact_way",
            ...
        ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant