我怎样才能在 django 1.8 中获得所有模型 [英] How can i get all models in django 1.8

查看:30
本文介绍了我怎样才能在 django 1.8 中获得所有模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 admin.py

from django.db.models import get_models, get_app

for model in get_models(get_app('myapp')):
    admin.site.register(model)

但是我收到警告说get_models 已被弃用

我如何在 django 1.8 中做到这一点

How can i do that in django 1.8

推荐答案

这应该可行,

from django.apps import apps
apps.get_models()

get_models 方法返回所有已安装模型的列表.您还可以传递三个关键字参数 include_auto_createdinclude_deferredinclude_swapped.

The get_models method returns a list of all installed models. You can also pass three keyword arguments include_auto_created, include_deferred and include_swapped.

如果您想获取特定应用的模型,您可以执行以下操作.

If you want to get the models for a specific app, you can do something like this.

from django.apps import apps
myapp = apps.get_app_config('myapp')
myapp.models

这将返回该应用程序模型的 OrderedDict 实例.

This will return an OrderedDict instance of the models for that app.

这篇关于我怎样才能在 django 1.8 中获得所有模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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