获取Django中所有安装的应用程序及其属性的列表 [英] Get a list of all installed applications in Django and their attributes

查看:86
本文介绍了获取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.

这篇关于获取Django中所有安装的应用程序及其属性的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆