0

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.

3
  • Have you read the Django documentation on creating and using fixtures? Commented Sep 20, 2016 at 19:34
  • Yes, I did. I have written fixtures earlier, but for this particular one I need to have contents of file in it which I have no idea how I can add into my JSON. Also not sure how will I be able to address conditions in my python file. Commented Sep 20, 2016 at 19:52
  • What kind of file are you trying to store? You need to provide a LOT more information about what you're trying to do otherwise this question will be closed for being too vague. Commented Sep 20, 2016 at 19:56

1 Answer 1

1

You should not be creating your fixtures manually. You should populate a test database with data then use manage.py 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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was doing the same till now, but I do not want to have a filepath for storing these files because every single time when a file is uploaded to my app, I do not want to make changes in my SCM.
Your staticfiles should not be version controlled. Usually, static files are stored in totally different place like CDN such as S3 or Akamai. The point is, the fixture stores a file path to that static location, not the file itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.