Login

Remove old fields on dumpdata generated json

Author:
facundo_olano
Posted:
October 21, 2011
Language:
Python
Version:
Not specified
Tags:
json loaddata fixture dumpdata migrate removed field
Score:
1 (after 1 ratings)

A small script that takes a manage.py dumpdata generated json file, and removes fields of the specified models. I needed this because i kept my initial data on a json file and after I removed a field on one of my models, the script wouldn't work anymore.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#dumpfix.py

import json
import sys

def fix_dump(in_file, out_file, model, *remove_fields):
    content = json.load(open(in_file))
    
    for item in content:
        if item['model'] == model:
            for field in remove_fields:
                if field in item['fields']:
                    del item['fields'][field]
    
    json.dump(content, open(out_file, 'w'))
    
if __name__ == "__main__":
    """ 
    Usage: 
    python dumpfix.py olddump.json newdump.json myapp.mymodel field1 field2 ...
    """
    fix_dump(*sys.argv[1:])

More like this

  1. Database migration and dump/load script by akaihola 8 years, 2 months ago
  2. JSON serializer supporting natural primary keys by bdauvergne 1 year, 3 months ago
  3. Export models as json by aconbere 8 years, 3 months ago
  4. Export Django data to datestamped tarball -- choose individual models for handy packaging and archiving by fish2000 4 years, 9 months ago
  5. Database cleanup by skyjur 4 years, 11 months ago

Comments

Please login first before commenting.