django.db.models.Model 的 Meta参数
参数 | 类型 | 说明 | 继承 |
abstract | boolean | 是否建表 | 不继承,子类自动充值为默认值(False) |
managed | boolean | 是否自动建表 | 不继承,子类自动充值为默认值(True) |
proxy | boolean | 是否为代理类(不建表) | 不继承,子类自动充值为默认值(False) |
1
2
3
4
5
6
7
|
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
1
2
3
|
class
AuthorProxy(Author):
class
Meta:
proxy
=
True
|
代理类子类 AuthorProxy2
1
2
|
class
AuthorProxy2(AuthorProxy):
pass
|
通过sqlall查看(django 1.6.5),建表如下:
1
2
3
|
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如下:
1
2
3
|
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), 因此适合的情形,比如给所有表增加一些共性字段,比如创建人等信息。