convert a django queryset into an array

后端 未结 2 1865
耶瑟儿~
耶瑟儿~ 2021-02-19 03:27

i would like convert a django queryset into an array like,

firstnames=Users.objects.values(\'firstnames\')

to get a result that looks like

相关标签:
2条回答
  • 2021-02-19 03:44
    def get_array(Table, column):
        rows = Table.objects.values(column)
        return [row[column] for row in rows]
    
    print get_array(Users, 'firstnames')
    
    0 讨论(0)
  • 2021-02-19 04:03

    Use QuerySet.values_list and specify flat=True:

    firstnames = Users.objects.values_list('firstnames', flat=True)
    firstnames = list(firstnames)
    
    0 讨论(0)
提交回复
热议问题