Skip to content

Commit

Permalink
new export function in admin site
Browse files Browse the repository at this point in the history
  • Loading branch information
JVUnderground committed Nov 26, 2016
1 parent 481e0af commit b7c596f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env
env/*
1 change: 1 addition & 0 deletions activate
Binary file modified db.sqlite3
Binary file not shown.
29 changes: 27 additions & 2 deletions poll/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
from django.contrib import admin
from .models import Question
from .models import Question, Answer
from django.http import HttpResponse
import csv

# Register your models here.

admin.site.register(Question)
def export_question_data(modeladmin, request, queryset):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="dados-votos.csv"'
writer = csv.writer(response)

header = ["Question ID", "Question Title", "Answer ID", "Social Score", "Economic Score"]
writer.writerow(header)

for question in queryset:
answers = Answer.objects.filter(question=question)
for answer in answers:
row = [question.id, question.title, answer.id, answer.social, answer.economic]
writer.writerow(row)

return response

class QuestionAdmin(admin.ModelAdmin):
list_display = ['id','title']
ordering = ['id']

actions = [export_question_data]


admin.site.register(Question, QuestionAdmin)
Binary file modified poll/admin.pyc
Binary file not shown.

0 comments on commit b7c596f

Please sign in to comment.