Created
April 1, 2012 23:19
-
-
Save lucassimon/2279369 to your computer and use it in GitHub Desktop.
Exportar para CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def export_subscriptions(self,request): | |
# coloca o mimetype da pagina para text/csv | |
response = HttpResponse(mimetype='text/csv') | |
# vai ser por download e o nome do arquivo sera listaInscricao | |
response['Content-Disposition'] = 'attachment; filename=listaInscricao.csv' | |
# resgata todos os objetos do model correte(subscription) | |
subscriptions = self.model.objects.all() | |
# inicializa a tupla vazia | |
tuplaWriteCsv = () | |
# inicializa a tupla temporaria | |
tempTupla = () | |
# arquivo para fazer escrita das tupla original, formato csv | |
out = csv.writer(response) | |
# faz a interacao do queryset subscriptions acima | |
for s in subscriptions: | |
# cria uma tupla contendo o nome,cpf,email,phone,criado em, e pago | |
tempTupla = (s.name,s.cpf,s.email,s.phone) | |
# coloca na tupla oficial o conteudo da tupla temporaria sempre adicionando uma virgula no final | |
tuplaWriteCsv += tempTupla, | |
# escrevendo as tuplas no arquivo | |
out.writerows(tuplaWriteCsv) | |
# retorna o response | |
return response |
Alterado!! Vallew pelas dicas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eu também inicializaria o response só no final da função.