Let's say I have this model:
class Student(models.Model):
YEAR_IN_SCHOOL_CHOICES = (
(FR, 'Freshman'),
(SO, 'Sophomore'),
(JU, 'Junior'),
(S, 'Senior'),
)
year_in_school = models.CharField(
max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES,
default=FRESHMAN,
)
If I use DRF to expose the value for the year_in_school field, I get the first parameter of the choice, for example: "FR".
How can I expose the second parameter "Freshman" instead of "FR"?
You can use model's get_FOO_display
method as source of serializer's field:
year_in_school = serializers.CharField(source='get_year_in_school_display')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With