You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using a SerializerMethodField to describe a list of values from a Django TextChoices enum, drf‑spectacular generates the referenced enum component name based on the serializer field name rather than the underlying enum class. In our case, even though we use a ChoiceField with choices from the ModelAction enum, the generated OpenAPI schema refers to the component as AllowedActionsEnum (derived from the field name) instead of ModelActionEnum (which is expected from the enum’s class name).
Does this behavior work as intended, or is there a recommended way to override the enum naming?
Minimal Reproducible Example:
model.py
class ModelAction(models.TextChoices):
SEND = "send", _("Send")
ACCEPT = "accept", _("Accept")
class ModelA(models.Model):
def get_action(self) -> list[ModelAction]:
return [ModelAction.SEND, ModelAction.ACCEPT]
serializers.py
class ModelASerializer(serializers.ModelSerializer):
allowed_actions = serializers.SerializerMethodField()
@extend_schema_field(
serializers.ListField(
child=serializers.ChoiceField(choices=ModelAction.choices)
)
)
def get_allowed_actions(self, obj) -> list[ModelAction]:
return obj.get_action()
class Meta:
model = ModelA
fields = ["allowed_actions"]
3. Generated OpenAPI Schema
The generated schema uses AllowedActionsEnum (derived from the serializer field name) with a definition similar to:
When using a
SerializerMethodField
to describe a list of values from a DjangoTextChoices
enum, drf‑spectacular generates the referenced enum component name based on the serializer field name rather than the underlying enum class. In our case, even though we use a ChoiceField with choices from the ModelAction enum, the generated OpenAPI schema refers to the component asAllowedActionsEnum
(derived from the field name) instead ofModelActionEnum
(which is expected from the enum’s class name).Does this behavior work as intended, or is there a recommended way to override the enum naming?
Minimal Reproducible Example:
model.py
serializers.py
3. Generated OpenAPI Schema
The generated schema uses
AllowedActionsEnum
(derived from the serializer field name) with a definition similar to:4. Expected schema
I want to the enum should be referenced as
ModelActionEnum
(based on the TextChoices class name):The text was updated successfully, but these errors were encountered: