我如何在Django 1.8中获得所有模型 [英] How can i get all models in django 1.8

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

问题描述

我在 admin.py

 中使用此代码从django.db.models导入get_models,get_app 

for get_models(get_app('myapp'))中的模型:
admin.site.register(model)

但是我收到警告,提示 get_models已过时



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

解决方案

这应该有效,

 从django.apps导入应用程序
apps.get_models()

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



如果要获取特定应用程序的模型,则可以执行以下操作。



<$来自django.apps的p $ p> 导入应用程序
myapp = apps.get_app_config('myapp')
myapp.models

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


I am using this code in my admin.py

from django.db.models import get_models, get_app

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

But i get warning that get_models is deprecated

How can i do that in django 1.8

解决方案

This should work,

from django.apps import apps
apps.get_models()

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

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

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

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