Django:如何找到 ORM 知道的模型列表? [英] Django : How can I find a list of models that the ORM knows?

查看:14
本文介绍了Django:如何找到 ORM 知道的模型列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Django 中,有什么地方可以获得 ORM 知道的模型列表或查找模型吗?

In Django, is there a place I can get a list of or look up the models that the ORM knows about?

推荐答案

简单的解决方案:

import django.apps
django.apps.apps.get_models()

默认apps.get_models()不包含

  • 为多对多关系自动创建模型,无需一个显式的中间表
  • 已换出的模型.

如果您还想包括这些,

django.apps.apps.get_models(include_auto_created=True, include_swapped=True)

在 Django 1.7 之前,改为使用:

Prior to Django 1.7, instead use:

from django.db import models
models.get_models(include_auto_created=True)

include_auto_created 参数确保通过 ManyToManyField 隐式创建的表也将被检索.

The include_auto_created parameter ensures that through tables implicitly created by ManyToManyFields will be retrieved as well.

这篇关于Django:如何找到 ORM 知道的模型列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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