Skip to content

Instantly share code, notes, and snippets.

@phrohdoh
Created February 3, 2018 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phrohdoh/c11acb38377477868299855ee3a32955 to your computer and use it in GitHub Desktop.
Save phrohdoh/c11acb38377477868299855ee3a32955 to your computer and use it in GitHub Desktop.
Filter out fixture object via jq for django's `loaddata` command
# Requires: jq 1.5
# Steps to use this filter:
# 1) dump your db via `python manage.py dumpdata -o mydata.json`
# 2) delete your db entirely via `rm db.sqlite3`
# 3) create a new db via `python manage.py migrate`
# 4) modify the `blacklist` below to include any `model`s you don't want to re-import (ones that `migrate` added, for example)
# 5) run your `mydata.json` file through this filter file like so `cat mydata.json | jq -f loaddata-helper.jq`, verify the output
# 6) run the above again this time redirecting stdout to a file (possibly overwriting your original json) `cat mydata.json | jq -f loaddata-helper.jq > myfixture.json`
# 7) create a django 'fixture' (which app you put it in doesn't matter, afaik) `mkdir -p apps/someapp/fixtures/ && mv myfixture.json $_`
# 8) import your fixture data via `python manage.py loaddata myfixture`
["contenttypes.contenttype", "admin.logentry"] as $blacklist
| [.[] | select(.model as $in | $blacklist | index($in) | not)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment