Building JSON object that can hold contents of file
<p>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.</p>

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

<p>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.</p>

<p>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.</p>

<p>You should not be creating your fixtures manually. You should populate a test database with data then use <a href="https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-dumpdata" rel="nofollow"><code>manage.py dumpdata</code></a> to export it. </p>

<p>Django <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.FileField" rel="nofollow"><code>FileField</code></a> and <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ImageField" rel="nofollow"><code>ImageField</code></a> both store a path to the <a href="https://docs.djangoproject.com/en/1.10/howto/static-files/" rel="nofollow"><code>staticfiles</code></a> location, they do not store the contents of the files directly.</p>

<p>If you're trying to store the contents of a text file, that can be held in a <a href="https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.TextField" rel="nofollow"><code>TextField</code></a>. 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 <code>\n</code>, etc.</p>

python json django database