Home > Software design >  How to load data outside Django view?
How to load data outside Django view?

Time:05-19

If i have some Django views like 'first', 'second' and so on, and i want to load some data outside this views but use it inside views.

Here is an example to understand my idea.

#function execution time is long, that is why i want to laod it only once when run programm.

fetched_data_from_postgres = some function which load data from postgres and return list with postgres data

def first(request):
    #and the i want to use that previous value in all my views
    fetched_data_from_postgres = do something
    return HttpResponse("first VIEW")

def second(request):
    #and the i want to use that previous value in all my views
    fetched_data_from_postgres = do something
    return HttpResponse("secondVIEW")

def third(request):
    #and the i want to use that previous value in all my views
    fetched_data_from_postgres = do something
    return HttpResponse("third VIEW")

this aproach working well when i run my django project like this python manage.py runserver but when i run with gunicorn or wsgi when i can specify workers count then when worker changes then this variable is lost and need to refresh page to get this previous worker to get that data. It's ridiculous.

Or maybe there is some other aproach to do this job?

CodePudding user response:

this approach doesn't work when you start project localy with python manage.py runserver, it only works once, when project starts, and then you need to reload project every time. This is because everything in any_views.py loads only once, when project starts, except your functions. So you have to restart your project, in order to refresh your fetched_data_from_postgres variable

the better approach, create a script fetch_script.py, move your function some function which load data from postgres and return list with postgres data inside it, and call it inside of views.py, not outside

CodePudding user response:

The best solution to avoid loading your data several times and to prevent this function fetched_data_from_postgres to run twice is to use a cache framework alongside Django. Check the documentation for more details: https://docs.djangoproject.com/en/stable/topics/cache/

It's relatively easy to set up and it should perfectly address your problem. If you feel it like overkill, then the question is do you really need speed? Are you sure you're not trying to prematurely optimize?

  • Related