Login

Remove old fields on dumpdata generated json

Author:
facundo_olano
Posted:
October 21, 2011
Language:
Python
Version:
Not specified
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.