【问题标题】:Add the new Dynamic Field in Odoo在 Odoo 中添加新的动态字段
【发布时间】:2015-10-21 06:02:39
【问题描述】:

是否可以在不使用 Odoo 模型类文件的情况下在 Odoo 中基于 Postgresql 中的视图添加新字段。 ?

【问题讨论】:

  • 你为什么要这样做?你能解释清楚吗?

标签: openerp odoo odoo-8 openerp-8


【解决方案1】:

您可以让模型从视图中获取数据。事实上,这已被报告广泛使用。

但可用的字段列表需要在模型中定义。

【讨论】:

  • 有没有办法动态创建上述模型?
【解决方案2】:

为了在 odoo 中创建动态视图,你最好参考这个 link

这里是解决方案

  • 给OpenERP对象指定参数_auto=False,不会自动创建_columns字典对应的表。

  • 添加一个方法 init(self, cr),该方法创建一个 PostgreSQL 视图,与 _columns 中声明的字段匹配。

Python 代码:

class xyz(osv.osv):
_name = "xyz"
_description = "xxx"

_auto = False

_columns = {
            'unique_id': fields.char('Employee ID', size=12),
            'employee_id': fields.many2one('table3', "Name"),
        }

def init(self, cr):
    openerp.tools.drop_view_if_exists(cr, 'table_preview')
    cr.execute("""
        create or replace view payslip_preview as (
                    SELECT * FROM crosstab('SELECT ps.id as id, emp.unique_id as unique_id, emp.id as employee_i
                    FROM table1 psl 
                    JOIN table2 ps ON (ps.id = psl.slip_id) 
                    JOIN table3 emp ON (emp.id = ps.employee_id) 
                   WHERE ps.state IN (''draft'') ORDER BY 1',
                    'SELECT id FROM table4 ORDER BY sequence') AS 
                    (
                        "id" int,
                        "unique_id" varchar(10),
                        "employee_id" int,
                    )
            )
        """)

【讨论】:

    猜你喜欢
    • 2015-03-04
    • 2016-08-21
    • 2023-01-19
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多