WitnessTruth
WitnessTruth

Reputation: 589

Get just the values without the fields queryset

I have this def in my views.py:

def listar_animais(request, pk):
vacas_no_lote = Animal.objects.filter(id_lote=pk, status=True, sexo=Sexo.F).values('id_animal', 'id_lote', 'id_raca')
return JsonResponse({ 'data' : list( vacas_no_lote )})

And I'm getting this return in JSON:

{
  "data": [
    {
      "id_animal": 2,
      "id_brinco": 5456,
      "id_raca": 3
    },
    {
      "id_animal": 4,
      "id_brinco": 5456,
      "id_raca": 3
    },
    {
      "id_animal": 5,
      "id_brinco": 5456,
      "id_raca": 3
    },
    {
      "id_animal": 9,
      "id_brinco": 5456,
      "id_raca": 1
    }
  ]
}

But I just want values, like this:

{
  'data': [
    ['1', '5471', 'Angus'],
    ['3', '5547', 'Nelore'],
    ['8', '6874', 'Brahman']
  ]
}

I need in this format because it's how will work with jQuery Datables as explained here: https://datatables.net/examples/data_sources/ajax

Upvotes: 1

Views: 36

Answers (1)

Alex
Alex

Reputation: 2484

You can use .values_list() instead of .values().

Upvotes: 3

Related Questions