开发者

Django / Python, using variablized object names

I'm co开发者_JAVA技巧nfused on how to do this, I'm passing in a dictionary with different values, such as "app" which is the name of the app (so that I can better decouple code for easier modularization).. so this is just a simplified example and its not working so I'm wondering how to use the variable "app" within the string that is calling the models import.. thanks for any advice

infodict={'app':'myappname'}
app = infodict['app']
from web1.app.models import modelname


You probably need something like importlib (Python 2.7). If you version of Python doesn't have importlib then you can use plain old __import__.

For example:

import sys
module_name = "web1.%s.models" % app
__import__(module_name)
models = sys.modules[module_name]
models.modelname


you should use contenttypes

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜