Returning get_status_display on json (Django)

0

I created an Issue.

link

I made a chart using Morris JS

link

Question:HowdoIreturnget_status_displayinjson?

importjsonfromdjango.db.modelsimportCountfromdjango.core.serializers.jsonimportDjangoJSONEncoderfromdjango.httpimportHttpResponsefrom.modelsimportProposaldefproposal_per_status_json(request):'''Quantidadedeorçamentosporstatus'''data=Proposal.objects.values('status')\.annotate(value=Count('status'))\.order_by('status').values('status','value')'''Precisareescreverodicionáriocomoscamposdográfico,quesão:'label'e'value'.'''data=[{'label':x['status'],'value':x['value']}forxindata]s=json.dumps(list(data),cls=DjangoJSONEncoder)returnHttpResponse(s)

link

[{"label": "a", "value": 11}, {"label": "c", "value": 9}, {"label": "co", "value": 28}, {"label": "elab", "value": 9}, {"label": "p", "value": 14}]

But I wanted to

[{"label": "aprovado", "value": 11}, {"label": "cancelado", "value": 9}, {"label": "concluído", "value": 28}, {"label": "em elaboração", "value": 9}, {"label": "pendente", "value": 14}]

STATUS_FILTER = (
    ('c', 'cancelado'),
    ('elab', 'em elaboração'),
    ('p', 'pendente'),
    ('co', 'concluido'),
    ('a', 'aprovado')
)
    
asked by anonymous 03.04.2016 / 01:47

1 answer

0
from orcamentos.utils.lists import STATUS_LIST
p=Proposal.objects.values('status')\
.annotate(value=Count('status'))\
.order_by('status').values('status', 'value')
lista=[]
for item in p:
    for choice in STATUS_LIST:
        if choice[0] == item['status']:
            lista.append({'label': choice[1], 'value': item['value']})

return lista
    
03.04.2016 / 04:44