INGl0R1AM0R1
INGl0R1AM0R1

Reputation: 1628

Json count query, how to pass only the counted value?

So basically, my issue is this json for the queried data2 passes the count number with the counted car brand, is there anyway i could only pass the counted part?

Here is my view, with the queries

class DadosDeGrafico1(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
    data1 = DataDB.objects.values_list('marca').distinct()
    data2 = DataDB.objects.values_list('marca').annotate(marcas=Count('marca'))
    contexto = {
    'data1':data1,
    'data2':data2
    }
    return Response(contexto)

enter image description here

Basically the result of the query should be only 2 and 6

Upvotes: 0

Views: 77

Answers (1)

Brad Martsberger
Brad Martsberger

Reputation: 1967

You can add values or values_list to get just the values you want

data2 = DataDB.objects.values('marca').annotate(marcas=Count('marca')).values_list('marcas')

A caution though, with this data structure you won't know which 'marca' each number goes with. The first and second queries are not guaranteed to return data in the same order. As you can see they are not in your example output.

Upvotes: 1

Related Questions