Home > Blockchain >  How to display the raw value of my queryset
How to display the raw value of my queryset

Time:02-03

I have this function, to show my querysets:

funcionarios_aniver = FuncionarioAux.objects.filter(FuncionarioDataNasc__gte=dia_um, FuncionarioDataNasc__lte=ultimo_dia)

But it returns the querysets in an array, for example:

<QuerySet [<FuncionarioAux: Alberto Santos>, <FuncionarioAux: Josney Arman>]>

I would like it to return the raw values, like:

Alberto Santos, Josney Arman

I already tried using .get() at the end of the function, but I got the following error:

funcionarios.models.FuncionarioAux.MultipleObjectsReturned: get() returned more than one FuncionarioAux -- it returned 2!

what can I do to resolve?

CodePudding user response:

You can evaluate it for list of objects:

list(FuncionarioAux.objects.filter(FuncionarioDataNasc__gte=dia_um, FuncionarioDataNasc__lte=ultimo_dia))

Or you can loop through it to access individual objects:

for o in FuncionarioAux.objects.filter(FuncionarioDataNasc__gte=dia_um, FuncionarioDataNasc__lte=ultimo_dia):
     print(o)

You can get a flat list of values as a values queryset:

foobar = FuncionarioAux.objects.filter(FuncionarioDataNasc__gte=dia_um, FuncionarioDataNasc__lte=ultimo_dia).values_list('name', flat=True) 
print(foobar)  # use list(foobar) to get list of names instead of values queryset

CodePudding user response:

You can use values_list so:

list(FuncionarioAux.objects.filter(FuncionarioDataNasc__gte=dia_um, FuncionarioDataNasc__lte=ultimo_dia).values_list('name', flat=True))
  • Related