Last active
May 19, 2022 20:37
-
-
Save rlskoeser/f9312d27eef352e16ccfa25a31132ca5 to your computer and use it in GitHub Desktop.
export django admin log entries to csv
This file contains hidden or 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
import csv | |
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION | |
# convert action codes to labels | |
action_label = {ADDITION: 'addition', CHANGE: 'change', DELETION: "deletion"} | |
with open('/tmp/django-logentries.csv', 'w') as csvfile: | |
writer = csv.writer(csvfile) | |
writer.writerow(['action_time', 'user', 'content_type', 'object_id', 'change_message', 'action_flag']) | |
for log in LogEntry.objects.all(): | |
writer.writerow([log.action_time.isoformat(), log.user, log.content_type, log.object_id ,log.change_message, action_label[log.action_flag]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment