django1.6, 通过字符串和get_app、get_model获得对应的object

只需要两行代码:

from django.db.models import get_model

get_model('auth', 'User')

下面是详细的过程展示:

>>> from django.db.models import get_app, get_models, get_model
# auth是django自带的app, get_app需要一个参数:app_name
>>> get_app('auth')
<module 'django.contrib.auth.models' from '/opt/project/easyui/lib/python2.7/site-packages/django/contrib/auth/models.pyc'>
>>> app = get_app('auth')
# 获得一个app的所有model
>>> get_models(app)
[<class 'django.contrib.auth.models.Permission'>, <class 'django.contrib.auth.models.Group'>, <class 'django.contrib.auth.models.User'> # 下面两种方式,得到的User是一样的, get_model需要两个参数,app_name和model_name
>>> get_model('auth', 'User')
<class 'django.contrib.auth.models.User'>
>>> from django.contrib.auth.models import User
>>> User
<class 'django.contrib.auth.models.User'>
>>> get_model('auth', 'User') == User
True
>>> u = get_model('auth', 'User')
>>> u.objects.all()[0]
<User: root>

django1.7, 可以使用上面的方法,也可以用新增加的django.apps来获取

>>> from django.apps import apps
>>> apps.get_app_config('auth')
<AuthConfig: auth>
# 注意得到的结果是迭代器(iterator)
>>> auth = apps.get_app_config('auth')
>>> auth.get_models()
<generator object get_models at 0x31422d0>
>>> for i in auth.get_models():
... print i
...
<class 'django.contrib.auth.models.Permission'>
<class 'django.contrib.auth.models.Group'>
<class 'django.contrib.auth.models.User'>
>>> auth.get_model('User')
<class 'django.contrib.auth.models.User'>
>>> User = auth.get_model('User')
>>> User.objects.all()[0]
<User: root>

注意,django1.7之前的版本没有django.apps

>>> import django
>>> django.get_version()
'1.6.5'
>>> from django.apps import apps
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named apps

django中通过model名字获取model的更多相关文章

  1. 【转】Django中使用POST方法获取POST数据

    1.获取POST中表单键值数据 如果要在django的POST方法中获取表单数据,则在客户端使用JavaScript发送POST数据前,定义post请求头中的请求数据类型: xmlhttp.setRe ...

  2. django中使用POST方法 获取POST数据

    在django中获取post数据,首先要规定post发送的数据类型是什么. 1.获取POST中表单键值数据 如果要在django的POST方法中获取表单数据,则在客户端使用JavaScript发送PO ...

  3. 在接口中的静态方法来获取model的实例对象

    直接先上代码: 接口代码 abstract class BaseModel { /** * @var null */ protected static $_instances = Array(); / ...

  4. Thymeleaf模板引擎与springboot关联后,在html中无法使用el表达式获取model存的值

    头部引入了thymeleaf <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thy ...

  5. web 架构 /http协议,状态码,django中常用命令

    什么是web应用? web应用 架构 :B/S架构 | C/S架构 网站:BS架构其实就是应用程序: B是浏览器 S是sever(实现了wsgi协议,实现了socket的服务端) + applicat ...

  6. SSH 框架controller向jsp传递List jsp中使用el表达式获取

    mvc可以使用ModelAndViev传递数据选择跳转的视图 controller中的代码, 把一个模拟的表单studentListSimulate传给ModelAndView @RequestMap ...

  7. django 中的聚合和分组 F查询 Q查询 事务cookies和sessions 066

    1 聚合和分组 聚合:对一些数据进行整理分析 进而得到结果(mysql中的聚合函数) 1aggregate(*args,**kwargs) : 通过对QuerySet进行计算 ,返回一个聚合值的字典. ...

  8. cookie,session 的概念以及在django中的用法,以及cbv装饰器用法

    cookie的由来: 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后 ...

  9. 获取Django中model字段名 字段的verbose_name

    obj._meta.fields 为关键 obj为model类 推荐使用函数 from django.apps import apps def getmodelfield(appname,modeln ...

随机推荐

  1. nodejs显现events.js:72抛出错误

    随着人们开始学习,我用nodejs过程中遇到如下列错误执行: events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EAD ...

  2. hdu 5077 NAND(暴力打表)

    题目链接:hdu 5077 NAND 题目大意:Xiaoqiang要写一个编码程序,然后依据x1,x2,x3的值构造出8个字符.如今给定要求生成的8个字符.问 说Xiaoqiang最少要写多少行代码. ...

  3. 声明式编程思想和EEPlat

    声明式编程定义 声明式编程(英语:Declarativeprogramming)它是一种编程范式.程相对立.它描写叙述目目标性质,让计算机明白目标,而非流程. 声明式编程不用告诉电脑问题领域.从而避免 ...

  4. Swift UI学习UITableView and protocol use

    Models: UserModel.swift Views: UserInfoCell.swift Controllers: RootViewController.swift, DetailViewC ...

  5. SQL优化策略高级优化经常使用-1(The Return Of The King)

    1 经常使用的优化策略 1.1    语句 1.1.1使用实际的列名 当我们查询SQL语句时.你是否觉得使用实际的列名比使用*更快呢?答案是肯定的. 为了证实这一点,感兴趣的朋友能够自己验证一下.我这 ...

  6. 使用SQLCMD在SQLServer执行多个脚本

    原文:使用SQLCMD在SQLServer执行多个脚本 概述: 作为DBA,经常要用开发人员提供的SQL脚本来更新正式数据库,但是一个比较合理的开发流程,当提交脚本给DBA执行的时候,可能已经有几百个 ...

  7. Appium之java API

    AppiumDriver getAppStrings() 默认系统语言相应的Strings.xml文件内的数据. driver.getAppStrings(String language) 查找某一个 ...

  8. ServletContext加入和访问

    (1)关于ServletContext认识: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/f ...

  9. HDU 2227 Find the nondecreasing subsequences(DP)

    Problem Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3 ...

  10. Repository、IUnitOfWork

    Repository.IUnitOfWork 在领域层和应用服务层之间的代码分布与实现 本来早就准备总结一下关于Repository.IUnitOfWork之间的联系以及在各层中的分布,直到看到田园里 ...