问题
Is it possible to add new field based on View in Postgresql in Odoo without using Odoo model class file. ?
回答1:
You can get have a model getting it's data from a view. In fact that's widely used by reports.
But the field list available needs to defined in the model.
回答2:
for creating dynamic views in odoo, you better refer this link
Here is the solution
Specify the parameter _auto=False to the OpenERP object, so no table corresponding to the _columns dictionary is created automatically.
Add a method init(self, cr) that creates a PostgreSQL View matching the fields declared in _columns.
Python code:
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,
)
)
""")
来源:https://stackoverflow.com/questions/33252030/add-the-new-dynamic-field-in-odoo