'How to export Django queryset to csv file

I have a values queryset of my values. how can convert it to a csv file.

Its important to export it by postgresql not python. because queryset have too many records.



Solution 1:[1]

import csv

query_set = django_query_set_you_have_made      

fields = ['Field1', 'Field2']

with open('my_file.csv', 'w') as file:
    write = csv.writer(file)
    write.writerow(fields)
    write.writerows(query_set)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David A