获取Django中所有安装的应用程序及其属性的列表
问题描述:
在我的Django网站中,我正在创建一个与网站中安装的其他应用程序动态交互的类。我必须对每个应用程序的每个字段进行操作。
In my Django website, I'm creating a class that interact dynamically with other applications installed in the website. I have to do a manipulation on each field of each application.
所以我想将所有安装的应用程序的名称保存在列表中,并获取每个应用程序的属性。有一种方法可以使用迭代器或其他东西来实现吗?
So I want to save the name of all installed applications in a list and get the attributes of each one. There is a way to do that using an iterator or something else ?
答
在Django 1.6及更低版本下。
Under Django 1.6 and below.
如果您想要所有型号,请尝试:
If you want all models, try:
from django.db.models import get_models
for model in get_models():
# Do something with your model here
print model.__name__, [x.name for x in model._meta.fields]
在Django 1.7及更高版本(感谢Colin Anderson):
Under Django 1.7 and above (thanks Colin Anderson):
$
I believe the older function still works.