Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Where shall I put 'initialization code' in a Django application?

$
0
0

I want some code in my Django app to be executed only once when server is started, not per request.

Specifically, I want to load some database tables into memory. These tables contain some 'metadata' that doesn't change per request. Like this:

ProductTypesData = None;def loadProductTypesData():     productTypes = ProductType.objects.all();     for ptype in productTypes:         ptype_data = {"id":ptype.id,"name": ptype.name,"description":ptype.desc         };         ProductTypesData.append(ptype_data);loadProductTypesData();

Where should I put this code?Other Q/A about django initialization suggest to put it in urls.py. But importing the models in urls.py doesn't seem logical to me.

Can I just put the code in models.py? Does it get executed only once (if it is acceptable to be executed not on django start)?


Viewing all articles
Browse latest Browse all 23160

Trending Articles