Я хочу получить ключ django rest choices вместо Value

Когда я выбираю выбор для типа степени, он возвращает значение выбора, но я хочу получить ключ. Например, когда я выбираю UNFINISHED_BACHELOR из вариантов выбора, возвращается Unfinished Bachelor's degree, но я хочу получить UNFINISHED_BACHELOR

class CandidateEducation(models.Model):
    class DegreeType(models.TextChoices):
        HIGH_SCHOOL = "High School"
        UNFINISHED_BACHELOR = "Unfinished Bachelor's degree"
        TWO_YEAR = "Two-year degree"

    degree_type = models.CharField(
        max_length=100, choices=DegreeType.choices, null=True, blank=True
    )
    degree_title = models.CharField(max_length=100, null=True, blank=True)
    institution = models.CharField(max_length=100, null=True, blank=True)

class CandidateEducationList(generics.ListCreateAPIView):
    serializer_class = CandidateEducationSerializer
    queryset = CandidateEducation.objects.all()

class CandidateEducationSerializer(serializers.ModelSerializer):
    class Meta:
        model = CandidateEducation
        fields = "__all__"

Result:
[
    {
        "id": 6,
        "degree_type": "Unfinished Bachelor's degree",   ----> Error
        "degree_title": "ABC",
        "institution": "aaa",
    }
]

Expected
[
    {
        "id": 6,
        "degree_type": "UNFINISHED_BACHELOR",  ---> Correct
        "degree_title": "ABC",
        "institution": "aaa",
    }
]

Если вы хотите, чтобы они были доступны вне модели, то определите класс choices вне модели, а затем используйте

DegreeType.HIGH_SCHOOL

Или, если вам нужен текст для чего-либо

DegreeType.HIGH_SCHOOL.label
Вернуться на верх