浅析django的abstract,proxy, managed
| 参数 | 类型 | 说明 | 继承 |
| abstract | boolean | 是否建表 | 不继承,子类自动充值为默认值(False) |
| managed | boolean | 是否自动建表 | 不继承,子类自动充值为默认值(True) |
| proxy | boolean | 是否为代理类(不建表) | 不继承,子类自动充值为默认值(False) |
proxy的继承特性,示例代码如下:
原始类Author
class Author(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField(blank=True,verbose_name='e-mail') def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)
代理类 AuthorProxy
class AuthorProxy(Author):
class Meta:
proxy = True
代理类子类 AuthorProxy2
class AuthorProxy2(AuthorProxy):
pass
通过sqlall查看(django 1.6.5),建表如下:
CREATE TABLE "books_authorproxy2" (
"author_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "books_author" ("id") DEFERRABLE INITIALLY DEFERRED
);
因此,可以断定,proxy在继承中的特性跟abstract一样。
将以上代码中的proxy换成managed ,并设置为 False,经测试,生成sql如下:
CREATE TABLE "books_authorproxy2" (
"authorproxy_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "books_authorpoxy" ("author_ptr_id") DEFERRABLE INITIALLY DEFERRED
);
即,managed不会继承到子类,子类会默认重置为True
用途
proxy or managed?
官方是这么说的:
So, the general rules are:
1. If you are mirroring an existing model or database table and don’t want all the original database table columns, use Meta.managed=False. That option is normally useful for modeling database views and tables not under the control of Django.
2. If you are wanting to change the Python-only behavior of a model, but keep all the same fields as in the original, use Meta.proxy=True. This sets things up so that the proxy model is an exact copy of the storage structure of the original model when data is saved.
即,通常:
1. 如果你要映射模型到已经存在的数据库,使用managed=False, 这适合不在django控制之下的数据库表和视图。
2. 如果只想要给模型修改python行为,而不需要改变任何字段,使用 proxy=True, 这会保持模型类的数据跟原始表结构一样(实际上就是一个表)
abstract
基本上,父类(abstract)的字段会拷贝到子类的每一个表中(如果子类没有设置Meta.abstract=True), 因此适合的情形,比如给所有表增加一些共性字段,比如创建人等信息。
浅析django的abstract,proxy, managed的更多相关文章
- Django的Model继承abstract,proxy,managed。。。
Django 中的 model 继承和 Python 中的类继承非常相似,只不过你要选择具体的实现方式:让父 model 拥有独立的数据库:还是让父 model 只包含基本的公共信息,而这些信息只能由 ...
- 浅析Django之session与cookie
浅析Django之session与cookie 1 session与cookie概述 原理: 由于HTTP协议是无状态,无连接的,当用户发起网路请求时,需要服务端能标识用户ID,用以存储用户相关信息, ...
- Django如何设置proxy
设置porxy的原因 一般情况下我们代理设置是针对与浏览器而言,通常只需在浏览器设置中进行配置,但它只针对浏览器有效,对我们自己编写的程序并任何效果,这时就需要我们在软件编码中加入代理设置. --- ...
- Django中CBV的执行顺序之源码解析
浅析Django中的CBV的执行顺序 下图为CBV方式的执行顺序,大概执行流程如下: 其中浅蓝色为在假设自己写的类,即Test类中没有dispatch方法的情况下的执行顺序,当自己的类中有dispat ...
- Spring依赖注入(IOC)那些事
小菜使用Spring有几个月了,但是对于它的内部原理,却是一头雾水,这次借着工作中遇到的一个小问题,来总结一下Spring. Spring依赖注入的思想,就是把对象交由Spring容器管理,使用者只需 ...
- Bootstrap 实战之响应式个人博客 (二)
阅读本博文前请参考:Bootstrap 实战之响应式个人博客 (一) 一.博客 1.结构 整体博客详情页的结构共包括四部分: 导航栏 博客主体内容 右侧栏:全局搜索框,广告位,推荐阅读 页尾 其中导航 ...
- Bootstrap 实战之响应式个人博客 (一)
一.示例 1.主页 2.博客详情页 3.在线地址 在线地址:入口 Addition:这里使用github-page将自己的静态项目免费部署到线上. 如果你只是做一些简单的静态项目做展示,付出这么大的时 ...
- Consul初体验
Preface Today I'm gonna implement a consul in my environment to discover service of MySQL da ...
- 【译】android的审计和hacking工具
原文:Best Android Tools For Security Audit and Hacking android系统占移动市场份额的80%且有恶意软件,这是一个问题.Hacker会对手机恶意操 ...
随机推荐
- usefull-url
http://www.johnlewis.com/ http://codepen.io/francoislesenne/pen/aIuko http://www.rand.org/site_info/ ...
- Yii2 RBAC 用到的表
Yii2 RBAC用到的四张auth表位于 vendor/yiisoft/yii2/rbac/migration文件夹里面,可以用migration生成 yii migrate --migration ...
- golang thrift 源码分析,服务器和客户端究竟是如何工作的
首先编写thrift文件(rpcserver.thrift),运行thrift --gen go rpcserver.thrift,生成代码 namespace go rpc service RpcS ...
- 【bzoj1191】 HNOI2006—超级英雄Hero
http://www.lydsy.com/JudgeOnline/problem.php?id=1191 (题目链接) 题意 有m个问题,n个锦囊妙计,每个锦囊妙计可以解决一个问题,每个问题有两个锦囊 ...
- ethtool使用记录
网卡出现很诡异的问题,把电脑连到一些交换机上是工作的,连到另外一些就不行...交换机上的link灯还时不时的闪一下,看起来像是在尝试连接. 用dmesg查看,看到下面的信息: [ 1112.92211 ...
- [IOS UICollectionView模版]
创建CollectionCell模版: 1.新建类CollectionCell继承自UICollectionViewCell 2.新建Xib,命名为CollectionCell.xib a.选中Col ...
- 获取Trustedinstalled权限.reg
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT*\shell\runas] @="获取TrustedInstaller权限&q ...
- what linux java cpu 100% ?
1.用top找到最耗资源的进程id [ bin]# toptop - 16:56:14 up 119 days, 6:17, 7 users, load average: 2.04, 2.07, 2. ...
- Java Observer 观察者
http://www.cnblogs.com/jaward/p/3277619.html 1.API 被观察者 java.util.Observable; public class Observabl ...
- 从系统的gallery获取图片
1 ) 效果演示: 2代码演示 布局代码: