Skip to content

Instantly share code, notes, and snippets.

@rockymeza
Created July 23, 2013 22:30
Show Gist options
  • Save rockymeza/6066742 to your computer and use it in GitHub Desktop.
Save rockymeza/6066742 to your computer and use it in GitHub Desktop.
Takes a model class and backs up all of the rows in a JSON file.
from django.conf import settings
from django.core.serializers import serialize
def backupmodel(model_class):
"""
Takes a model class and backs up all of the rows in a JSON file.
"""
log.info("Backing up %s", model_class)
file_name = '{prefix}_{time}.json'.format(
prefix=modelclass._meta.module_name,
time=now().strftime('%F-%s'),
)
with open(os.path.join(settings.PROJECT_PATH, '..', 'tmp', file_name), 'w') as f:
f.write(serialize('json', modelclass.objects.all()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment