作为答案
Django: Get model from string?
秀,你需要
app_name
还是一根绳子
AppConfig
对象从中获取模型
model_name
.
但是,您可以使用
apps.get_app_configs()
反复
AppConfig
实例,所以类似这样的事情是可能的:
from django.apps import apps
for app_conf in apps.get_app_configs():
try:
foo_model = app_conf.get_model('foo')
break # stop as soon as it is found
except LookupError:
# no such model in this application
pass
请注意,此iterable将包括中的基本django应用程序
INSTALLED_APPS
喜欢
admin
,
auth
,
contenttypes
,
sessions
,
messages
和
staticfiles
.