0

I have a django project where I need to load some external data (sparse matrix, lists, dictionaries) that will be used for a specific view. Loading these files each time the view is requested is not optimal because it takes time to load them, so I wanted to know if there was a way to load everything only once while the project is being loaded with the command python manage.py runserver and thus have those loaded files as global elements of the project. Is it possible to do so? If it's not possible to do it with the runserver command, what alternative do I have?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Babas
  • 377
  • 3
  • 16
  • You could load the data with the `settings.py` file, i believe it's only executed once on every server start – Johan Apr 12 '19 at 08:00
  • sounds interesting, would load everything and then convert them to global variables? – Babas Apr 12 '19 at 12:09
  • You could load `from django.conf import settings` and then access it from `settings.VARIABLE_NAME`, but I believe there are other ways to load data once every server start as well. – Johan Apr 12 '19 at 12:12
  • However, I must admit that my approach is a bit of a bad practice, since it doesn't really have anything to do with settings. You should probably try to adapt another way, such as [this](https://stackoverflow.com/q/39523075/10400050) or [this answer](https://stackoverflow.com/q/2680902/10400050) – Johan Apr 12 '19 at 12:17

0 Answers0