Meta-Parameters django.db.models.Model
parameter | Types of | Explanation | inherit |
abstract | boolean | Whether to build the table | Not inherited, the subclass automatically recharge the default value (False) |
managed | boolean | Whether to automatically create a table | Not inherited, the subclass automatically recharge the default value (True) |
proxy | boolean | Whether the proxy class (not to build the table) | Not inherited, the subclass automatically recharge the default value (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)
|
Acting classes AuthorProxy
1
2
3
|
class
AuthorProxy(Author):
class
Meta:
proxy
=
True
|
Proxy Subclass AuthorProxy2
1
2
|
class
AuthorProxy2(AuthorProxy):
pass
|
By sqlall View (django 1.6.5), to build the table below:
1
2
3
|
CREATE
TABLE
"books_authorproxy2"
(
"author_ptr_id"
integer
NOT
NULL
PRIMARY
KEY
REFERENCES
"books_author"
(
"id"
) DEFERRABLE INITIALLY DEFERRED
);
|
Therefore, it can be concluded, proxy characteristics in succession with the same abstract.
The above proxy code into Managed, and set to False, tested, sql generated as follows:
1
2
3
|
CREATE
TABLE
"books_authorproxy2"
(
"authorproxy_ptr_id"
integer
NOT
NULL
PRIMARY
KEY
REFERENCES
"books_authorpoxy"
(
"author_ptr_id"
) DEFERRABLE INITIALLY DEFERRED
);
|
That is, managed not inherited by subclasses, subclasses will be reset to default True
use
proxy or managed?
The official said so:
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.
That is, generally:
1. If you want to map to existing database model, the use of managed = False, which does not fit django database tables and views under control.
2. If you want to give the model to modify the behavior of python, without changing any fields, using proxy = True, this will keep the model class with the data structure as the original table (actually a table)
abstract
Basically, the parent class (abstract) field of each table will be copied to the sub-class (subclass if not set Meta.abstract = True), and therefore suitable for the case, such as to add all the common tables fields created by such and other information.
parameter | Types of | Explanation | inherit |
abstract | boolean | Whether to build the table | Not inherited, the subclass automatically recharge the default value (False) |
managed | boolean | Whether to automatically create a table | Not inherited, the subclass automatically recharge the default value (True) |
proxy | boolean | Whether the proxy class (not to build the table) | Not inherited, the subclass automatically recharge the default value (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)
|
Acting classes AuthorProxy
1
2
3
|
class
AuthorProxy(Author):
class
Meta:
proxy
=
True
|
Proxy Subclass AuthorProxy2
1
2
|
class
AuthorProxy2(AuthorProxy):
pass
|
By sqlall View (django 1.6.5), to build the table below:
1
2
3
|
CREATE
TABLE
"books_authorproxy2"
(
"author_ptr_id"
integer
NOT
NULL
PRIMARY
KEY
REFERENCES
"books_author"
(
"id"
) DEFERRABLE INITIALLY DEFERRED
);
|
Therefore, it can be concluded, proxy characteristics in succession with the same abstract.
The above proxy code into Managed, and set to False, tested, sql generated as follows:
1
2
3
|
CREATE
TABLE
"books_authorproxy2"
(
"authorproxy_ptr_id"
integer
NOT
NULL
PRIMARY
KEY
REFERENCES
"books_authorpoxy"
(
"author_ptr_id"
) DEFERRABLE INITIALLY DEFERRED
);
|
That is, managed not inherited by subclasses, subclasses will be reset to default True
use
proxy or managed?
The official said so:
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), 因此适合的情形,比如给所有表增加一些共性字段,比如创建人等信息。