Thanks!
I ended up adding a @classmethod to my enum to get the names back, then in
my serializer:
state = serializers.ChoiceField(choices=Job.JobState.names())
Which worked great.
On Sat, Aug 21, 2021 at 12:40 PM Carl Nobile <[email protected]> wrote:
> In this part do this:
>
> class JobState(IntEnum):
> RECEIVED = 0
> PROCESSING = 1
> DONE = 2
> FAILED = 3
> INT_ENUM = {'RECEIVE': RECEIVE, 'PROCESSING': PROCESSING,
> 'DONE': DONE, 'FAILED': FAILED}
>
> Then use the INT_ENUM in your serializer. This may not work exactly
> in your case, however, it could be useful.
> I do this all the time, but I don't make another embedded class, I don't
> see any real need for it. Just use the standard Django way to create an
> iterator for a model field.
> https://docs.djangoproject.com/en/3.2/ref/models/fields/#django.db.models.Field.choices
>
> ~Carl
>
> On Sat, Aug 21, 2021 at 2:44 PM Adam Fletcher <[email protected]> wrote:
>
>> Hi DRFers,
>>
>> I have a model like this:
>>
>> class Job(Model):
>>
>> class JobState(IntEnum):
>> RECEIVED = 0
>> PROCESSING = 1
>> DONE = 2
>> FAILED = 3
>>
>> @classmethod
>> def choices(cls: Any) -> List[Tuple[int, str]]:
>> return [(key.value, key.name) for key in cls]
>>
>> state = IntegerField(
>> choices=JobState.choices(),
>> default=JobState.RECEIVED,
>> )
>>
>> I have a ModelSerializer:
>> class JobSerializer(serializers.ModelSerializer):
>> class Meta:
>> model = Job
>> fields = [
>> "state",
>> ]
>> How do I get the OpenAPI schema to show the _names_ of the fields, not
>> the enum values?
>>
>> My schema generates to:
>>
>> "state": {
>> "enum": [
>> 0,
>> 1,
>> 2,
>> 3
>> ],
>> "type": "integer",
>> "minimum": -2147483648,
>> "maximum": 2147483647
>> },
>>
>> Any ideas on how to make the enum use the name, not the value?
>>
>> Thanks!
>> -Adam
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-rest-framework/CAEP43uOWYYhQ8hH7xt0qqOk9E4D9rW0Z0OzDyYqKvnE0pg2jSQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-rest-framework/CAEP43uOWYYhQ8hH7xt0qqOk9E4D9rW0Z0OzDyYqKvnE0pg2jSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> --
>
> -------------------------------------------------------------------------------
> Carl J. Nobile (Software Engineer)
> [email protected]
>
> -------------------------------------------------------------------------------
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQKr78hh1u2EofSJSRUPuJ-4Yr0W3jBHfS3cJ3SeWgbFFg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CAGQqDQKr78hh1u2EofSJSRUPuJ-4Yr0W3jBHfS3cJ3SeWgbFFg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-rest-framework/CAEP43uOt3RUEjgfxSz_yV-uRy4eUy%2BpMXdVR8ewA8oNguGRTUA%40mail.gmail.com.