-
Notifications
You must be signed in to change notification settings - Fork 277
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
Returning an Enum
from a SerializerMethod
#1383
Comments
Enum
from a SerializerMethod
Enum
from a SerializerMethod
_KnownPythonTypes = typing.Type[typing.Union[
str, float, bool, bytes, int, dict, UUID, Decimal, datetime, date, time,
timedelta, IPv4Address, IPv6Address,
]]
Edit: This only partially seems to work. |
Switching to @extend_schema_field(Color)
def get_color(self, instance) -> Any:
return Color.Red.value |
I think what's causing the error is the allOf: line. My other |
@tfranzel If there is a proper way to do this, I'm also happy to add a FAQ entry about it. 😃 |
Okay, not sure why I didn't think of this at first, but using an from enumfields.drf import EnumField
class Item(Model):
name = TextField()
@unique
class Color(Enum):
Red = "red"
Green = "green"
Blue = "blue"
class ItemSerializer(ModelSerializer):
color = SerializerMethodField()
@extend_schema_field(EnumField(Color))
def get_color(self, instance) -> str:
return Color.Red.value
class Meta:
model = Item
fields = "__all__" If this is indeed the proper way to do this, I'm happy to make an FAQ. Also, I could potentially see someone who doesn't use https://github.com/akx/django-enumfields2 wanting to export a simple Maybe natively supporting the |
Is it possible to express this:
We've been able to use
Enum
s directly withinModel
s, using https://github.com/akx/django-enumfields2 and that translates properly into an OpenAPIenum
.Is there a way to expose similar functionality through a
SerializerMethod
in a waydrf-spectacular
can process?The text was updated successfully, but these errors were encountered: