zoukankan      html  css  js  c++  java
  • 浅析django的abstract,proxy, managed

    django.db.models.Model 的 Meta参数
    参数 类型 说明 继承
    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), 因此适合的情形,比如给所有表增加一些共性字段,比如创建人等信息。

  • 相关阅读:
    Grunt构建工具插件篇——之less工具
    Grunt构建工具能做哪些事?
    Grunt-几个常用的任务配置,加载,执行的写法
    单元测试任务包括哪些?
    单元测试的概念
    分享Grunt.js配置: watch + liveReload 实时监测文件变化自动刷新浏览器
    如何使用Grunt(好文)
    grunt安装详解及失败处理
    利用 Grunt (几乎)无痛地做前端开发 (一)之单元测试
    Grunt实现自动化单元测试
  • 原文地址:https://www.cnblogs.com/Tommy-Yu/p/4096569.html
Copyright © 2011-2022 走看看