Created
November 8, 2011 15:08
-
-
Save dchaplinsky/1347977 to your computer and use it in GitHub Desktop.
Script for dumping only particular fields of particular django models to JSON
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 sys, os | |
| sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), | |
| "../")) | |
| os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') | |
| from django.core import serializers | |
| from django.db.models import get_model | |
| from django.template.defaultfilters import slugify | |
| MODELS_TO_DUMP = ( | |
| { | |
| "model": "foo.Foobar", | |
| "fields": ("pk", "title", "slug", "description"), | |
| }, | |
| ) | |
| for m in MODELS_TO_DUMP: | |
| model = get_model(*(m["model"].split("."))) | |
| with open("%s.json" % slugify(m["model"]), "w") as f: | |
| f.write(serializers.serialize("json", model.objects.all(), | |
| fields = m["fields"])) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment