[Django]-Convert a django queryset into an array

36👍

Use QuerySet.values_list and specify flat=True:

firstnames = Users.objects.values_list('firstnames', flat=True)
firstnames = list(firstnames)

1👍

def get_array(Table, column):
    rows = Table.objects.values(column)
    return [row[column] for row in rows]

print get_array(Users, 'firstnames')

Leave a comment