Django:从字符串获取模型? [英] Django: Get model from string?
问题描述
在Django中,您可以指定以下关系:
author = ForeignKey('Person')
然后内部必须将字符串Person转换为模型 Person
。
这样做的功能在哪里?我想使用它,但我找不到它。
从Django 1.9开始该方法是
django。 apps.AppConfig.get_model(model_name)
。
- danihp
从Django 1.7起,
django.db.models.loading
已被弃用(将在1.9中删除),有利于新的应用程序加载系统。
- Scott Woodall
找到它。它定义在这里:
从django.db.models.loading import get_model
/ pre>
定义为:
def get_model(self,app_label ,model_name,seed_cache = True):
In Django, you can specify relationships like:
author = ForeignKey('Person')
And then internally it has to convert the string "Person" into the model
Person
.Where's the function that does this? I want to use it, but I can't find it.
解决方案As of Django 1.9 the method is
django.apps.AppConfig.get_model(model_name)
.
-- danihp
As of Django 1.7 the
django.db.models.loading
is deprecated (to be removed in 1.9) in favor of the the new application loading system.
-- Scott Woodall
Found it. It's defined here:
from django.db.models.loading import get_model
Defined as:
def get_model(self, app_label, model_name, seed_cache=True):
这篇关于Django:从字符串获取模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!