VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > python入门教程 >
  • odoo 开发入门教程系列-模型和基本字段

模型和基本字段

在上一章的末尾,我们创建一个odoo模块。然而,此时它仍然是一个空壳,不允许我们存储任何数据。在我们的房地产模块中,我们希望将与房地产相关的信息(名称(name)、描述(description)、价格(price)、居住面积(living area)…)存储在数据库中。odoo框架提供了数据库交互的工具

开始练习前,请确保estate模块已被安装,也就是说必须以installed的状态出现在Apps列表中,如下

对象关系映射(Object-Relational Mapping)

参考: 和本主题关联文档可参考 Models API.

ORM 层是odoo的一个关键组件。该层避免了手动写入大部分SQL并提供可扩展性和安全服务.

业务对象被定义为继承于 Model的Python类。可以通过在定义中设置属性来配置model。最重要的属性为 _name,该属性定义了model在odoo系统中的属性。以下为model的最小化定义:

from odoo import models

class TestModel(models.Model):
    _name = "test.model"

该定义足够ORM生成一张名为test_model的表。model _name中的 . 会被ORM自动化转为_ 。按约定所有的model位于一个名为 models 的目录,并且每个mode被定义为一个Python文件。

来看下 crm_recurring_plan 表是怎么定义的,以及对应Python文件是怎么导入的:

  1. 在 odoo/addons/crm/models/crm_recurring_plan.py 中定义model(源码链接)

    # -*- coding: utf-8 -*-
    # Part of Odoo. See LICENSE file for full copyright and licensing details.
    
    from odoo import fields, models
    
    
    class RecurringPlan(models.Model):
        _name = "crm.recurring.plan"
        _description = "CRM Recurring revenue plans"
        _order = "sequence"
    
        name = fields.Char('Plan Name', required=True, translate=True)
        number_of_months = fields.Integer('# Months', required=True)
        active = fields.Boolean('Active', default=True)
        sequence = fields.Integer('Sequence', default=10)
    
        _sql_constraints = [
            ('check_number_of_months', 'CHECK(number_of_months >= 0)', 'The number of month can\'t be negative.'),
        ]
    
  2. crm/models/__init__.py中导入crm_recurring_plan.py (源码链接)

    # -*- coding: utf-8 -*-
    # Part of Odoo. See LICENSE file for full copyright and licensing details.
    
    from . import res_users
    from . import calendar
    from . import crm_lead
    from . import crm_lost_reason
    from . import crm_stage
    from . import crm_team
    from . import ir_config_parameter
    from . import res_config_settings
    from . import res_partner
    from . import digest
    from . import crm_lead_scoring_frequency
    from . import utm
    from . import crm_recurring_plan
    
  3. crm/__init__.py中导入models包 (源码链接)

    # -*- coding: utf-8 -*-
    # Part of Odoo. See LICENSE file for full copyright and licensing details.
    
    from . import controllers
    from . import models
    from . import report
    from . import wizard
    
    from odoo import api, SUPERUSER_ID
    

练习

创建estate_property表的最小化模型

  1. 在 odoo14/custom/estate/models/estate_property.py 中定义model

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from odoo import model
    
    class EstateProperty(models.Model):
        _name = 'estate.property'
    
  2. estate_property.py 在odoo14/custom/estate/models/__init__.py中导入

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from . import estate_property
    
  3. estate/__init__.py中导入 models

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    from . import models
    

重启odoo服务

python odoo-bin --addons-path=custom,odoo/addons -r myodoo -w test123 -d odoo -u estate

-u estate 表示更新 estate 模块,也就是说ORM将应用数据库模式变更。

启动过程中可以看到类似以下告警日志:

...
2022-12-14 06:46:02,771 23792 WARNING odoo odoo.models: The model estate.property has no _description 
2022-12-14 06:46:02,920 23792 WARNING odoo odoo.models: The model estate.property has no _description 
...
2022-12-14 06:46:03,498 23792 WARNING odoo odoo.modules.loading: The model estate.property has no access rules, consider adding one...
...
...

以防万一,可以看下到数据库看下表是否创建成功。pgAmin查看路径:Servers -> PostgreSQL 12 -> Databases (x) ->数据库名 -> Schemas -> public -> Tables

模型字段(Model Fields)

参考: 该主题相关文档可参考 Fields API

字段用于定义model可以存储啥及在哪里存储。 Fields被定义为model类的属性:

from odoo import fields, models

class TestModel(models.Model):
    _name = "test.model"
    _description = "Test Model"

    name = fields.Char()

name 字段被定义为Char,代表Python unicode的 str 和SQL的 VARCHAR.

有两大类领域字段:‘简单’字段--直接存储在模型表中的原子值,形如BooleanFloatCharTextDate 和Selection, ‘关系型’ 字段--连接相同或者不同模型的记录。

给模型表estate_property添加字段

添加以下字段到表中

Field Type
name Char
description Text
postcode Char
date_availability Date
expected_price Float
selling_price Float
bedrooms Integer
living_area Integer
facades Integer
garage Boolean
garden Boolean
garden_area Integer
garden_orientation Selection

The garden_orientation 字段必须有4种可选值:‘North’, ‘South’, ‘East’ 和‘West’。Selection(选择列表)定义为元组列表,查看示例

修改odoo14/custom/estate/models/estate_property.py文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from odoo import models,fields


class EstateProperty(models.Model):
    _name = 'estate.property'
    _description = 'estate property table'

    name = fields.Char(size=15)
    description = fields.Text()
    postcode = fields.Char(size=15)
    date_availability = fields.Datetime('Availability Date')
    expected_price = fields.Float('expected price', digits=(8, 2)) # 最大8位,小数占2位
    selling_price = fields.Float('selling price', digits=(8, 2))
    bedrooms = fields.Integer()
    living_area = fields.Integer()
    facades = fields.Integer()
    garage = fields.Boolean('garage')
    garden = fields.Boolean('garden')
    garden_area = fields.Integer()
    garden_orientation = fields.Selection(
        string='Orientation',
        selection=[('north', 'North'), ('south', 'South'), ('east', 'East'), ('West','West')],
        help="garden orientation"
    )

重启odoo服务

python odoo-bin --addons-path=custom,odoo/addons -r myodoo -w test123 -d odoo -u estate

数据库中验证

常见属性

现在假设要求 name 和expected_price字段值不为null,所以需要对其修改,如下,添加字段属性配置required=True

name = fields.Char(required=True)
expected_price = fields.Float('expected price', digits=(8, 2),  required=True) # 最大8位,小数占2位

修改后重启odoo服务。

有些属性是所有字段都拥有的,最常见的几个属性如下:

  • string (str, default: 字段名称)

    UI上显示为字段的label (用户可见).

  • required (bool, default: False)

    如果为 True, 表示该字段值不能为空。创建记录时必须拥有默认值或给定的值。

  • help (str, default: '')

    UI上为用户提供long-form 帮助提示

  • index (bool, default: False)

    要求odoo在该列上创建数据库索引

自动创建的字段(Automatic Fields)

参考: 该话题相关文档可参考 Automatic fields.

odoo会在所有model(当然,也可以配置禁止自动创建某些字段)中创建少数字段。这些字段有系统管理并且不能写,但是可以读取,如果必要的话:

  • id (Id)

    model记录的唯一标识

  • create_date (Datetime)

    记录创建日期

  • create_uid (Many2one)

    记录创建人

  • write_date (Datetime)

    记录最后修改时间

  • write_uid (Many2one)

    记录最后修改人



相关教程