[Python] Best place to initialize a variable from a postgres database table after django...

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have a django project where I have some database tables.

    One of the database tables is designed to store messages and their titles. This helps me to create/alter these messages from my django-admin.

    Now I want to initialize a variable (as a dictionary) from this table as follows :

    MY_MSGS = {record.name : {'title':record.title, 'message':record.message} for record in MyTable.objects.all()}


    This must happen at the server startup for now. MY_MSGS must be accessible to the different view-files.

    Later I would want to periodically update MY_MSGS by reading MyTable again.

    So I want that My_MSGS behaves as a all global to all my view-files and should be initialized after the startup is complete.

    FYI I have multiple view-files that are all imported from views.py. Also this is a very small table with just about maximum 15 messages and so I do not mind holding this data in memory

    Continue reading...

Compartilhe esta Página