Skip to content

How to get QuerySet values separately?

How I can to get values from QuerySet separately? The result of my QuerySet now is <QuertSet[<value: 5>, <value:90>]> How I can get 5, 90 to separate variable ?

Answer

There are many ways you can acheive it. For example:

for item in queryset:
    print(item.field_name)

or using values_list():

for item in queryset.values_list('field_name', flat=True):
    print(item)

If you want the first two values of a queryset, then do this:

values = queryset.values_list('field_name', flat=True)
variable_one = values[0]
variable_two = values[1]