我想将django查询集转换为数组,如
firstnames=Users.objects.values('firstnames')
获得看起来像
的结果firstnames = ["Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Laura", "Anne"];
有什么见解吗? 问候 约什
答案 0 :(得分:12)
使用QuerySet.values_list
并指定flat=True
:
firstnames = Users.objects.values_list('firstnames', flat=True)
firstnames = list(firstnames)
答案 1 :(得分:0)
def get_array(Table, column):
rows = Table.objects.values(column)
return [row[column] for row in rows]
print get_array(Users, 'firstnames')