- 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
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Please login first before commenting.