Anil Pediredla Anil Pediredla - 1 month ago 6x
JSON Question

Building JSON object that can hold contents of file

I have to save contents of a python file to my database. I am writing fixtures so that I can load that to my database from Django project.

I have no idea where to begin with. The size is an issue that I need to address for my JSON object.

Edit: My app features an upload option where user can upload their python file which does some action, and gives some result. If the user is pleased with my app's performance he can store his script (python file) and can run it anytime.

So far, I am using a static file location to store the python file. I feel like not updating my SCM whenever a file is uploaded to the system, instead have it store in my database in assumption that I will be having a infinite storage space.


You should not be creating your fixtures manually. You should populate a test database with data then use dumpdata to export it.

Django FileField and ImageField both store a path to the staticfiles location, they do not store the contents of the files directly.

If you're trying to store the contents of a text file, that can be held in a TextField. To represent this in JSON, it will most likely need to be encoded to UTF8 to handle special characters (pretty quotes, accents, etc.) and escaped so that line endings are turned into \n, etc.