Skip to content

Instantly share code, notes, and snippets.

@rlskoeser
Last active May 19, 2022 20:37
Show Gist options
  • Save rlskoeser/f9312d27eef352e16ccfa25a31132ca5 to your computer and use it in GitHub Desktop.
Save rlskoeser/f9312d27eef352e16ccfa25a31132ca5 to your computer and use it in GitHub Desktop.
export django admin log entries to csv
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