Add the new Dynamic Field in Odoo

限于喜欢 提交于 2020-01-03 03:50:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!