2015-10-21 101 views

回答

1

你可以有一個模型從視圖中獲取數據。事實上,這被報告廣泛使用。

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

+0

有什麼辦法可以動態創建上述模型? – Naitik

1

用於創建odoo動態視圖,你最好請參閱本 link

下面是解

  • 指定參數_auto =假的OpenERP的對象,所以相當於沒有表_columns字典是自動創建的。

  • 添加一個方法init(self,cr),該方法創建與_columns中聲明的字段匹配的PostgreSQL視圖。

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, 
        ) 
      ) 
     """)