Return choices label
vidz1979 opened this issue · 1 comments
vidz1979 commented
How do I return choices label?
columns = ['status']
return 1 and I want to return the text label 'ACTIVE'.
vidz1979 commented
Solved it!
from django.db.models import Value, When, Case, CharField
def display_value(choices, field):
options = [When(**{field: k, "then": Value(v)}) for k, v in choices]
return Case(*options, output_field=CharField())
class ColaboradorDataView(ServerSideDatatableView):
my_fields = {
"tipo_contrato_desc": display_value(TipoContrato.choices, "tipo_contrato"),
"status_desc": display_value(ColaboradorStatus.choices, "status"),
}
queryset = Colaborador.objects.annotate(**my_fields)
columns = [
"id",
"pessoa__nome",
"equipe__nome",
"cargo__nome",
"tipo_contrato_desc",
"status_desc",
]