Karthik
Karthik

Reputation: 2581

Return Django Model ChoiceField label in a queryset

The queryset of a model which includes ChoiceField returns the 'value' of the ChoiceField. But I want the 'Label' of the ChoiceField. The queryset is for an ajax request to render a DataTable table. So I'm not able to use the form template get_FOO_display().

class MainBase(models.Model):

    class MainBaseChoices(models.TextChoices):
        ACTIVE = '1', 'Active'
        DEFUNCT = '2', 'Defunct'
        DUPE = '3', 'Duplicate'
        INVALID = '4', 'Invalid'

    user_id = models.AutoField(primary_key=True)
    user_name = models.CharField(max_length=200, null=False, blank=False)
    user_status = models.CharField(max_length=10, null=False, blank=False, choices=MainBaseChoices.choices, default=MainBaseChoices.ACTIVE)

    ....

Upvotes: 0

Views: 314

Answers (1)

ahmad dana
ahmad dana

Reputation: 41

I think your problem here is that the ajax call might want the response to be in JSON ? if yes, you might want to customize your end point with django rest framework to do return JSON. here's a link to how to do so with DRF https://www.django-rest-framework.org/api-guide/fields/#choicefield

Upvotes: 1

Related Questions