0

我需要在笔记本表上显示笔记本项目的记录,但显示的记录与项目不相符

我的 py :

class notebook_project(osv.osv):
    _name = "notebook.project"
    _description = "Notebook Project ID"

    def onchange_project(self, cr, uid, ids, project, arg, context=None):
        if project :
            proj = self.pool.get('project.project').browse(cr, uid, project, context=context)
            return {'value': {'name': proj.name}}
        return {}
    _columns = {
        'name' : fields.char('Name', size=64),
        'project' : fields.many2one('project.project', 'Project'),
        'notebook_project_lines' : fields.one2many('notebook.project', 'notebook_project_id', 'Members Lines'),
        'notebook_project_id': fields.many2one('notebook.project', 'Parent Project', ondelete='cascade', select=True),
        'member' : fields.many2one('hr.employee', 'Members'),

        }

notebook_project()


class notebook_sheet(osv.osv):
    _name = "notebook.sheet"
    _description = "Notebook Project Sheet"

    def onchange_notebook_project(self, cr, uid, ids, notebook_project, context=None):
        res = {}
        employee_lines = []
        if not notebook_project : return {}
        if notebook_project :
            notebook_project_obj = self.pool.get('notebook.project').browse(cr, uid, notebook_project)
            for p in notebook_project_obj.notebook_project_lines:
            employee_lines.append((0,0,{'notebook_sheet_lines':p.id
            }))#this dict contain keys which are fields of one2many field 
            res['notebook_sheet_lines']=employee_lines
            return res

    def onchange_project(self, cr, uid, ids, project, context=None):
        if project :
            proj = self.pool.get('project.project').browse(cr, uid, project, context=context)
            return {'value': {'name': proj.name}}
        return {}

    #def create(self, cr, user, vals, context={}):
    #first model
    #   notebook_project_obj = self.pool.get('notebook.project')
    #browse and get o2m fields, according to your selected project(id)
    #   notebook_project_lines = notebook_project_obj.browse(cr, user, ['notebook_project_id'])[0].lines
    #copy first o2m model to second o2m model
    #   for line in notebook_project_lines :
    #       vals['notebook_sheet_lines'].append([0, False, {'notebook_project_lines':line.employee_id.id,}])
    #   return super(notebook_sheet, self).create(cr, user, vals, context)

    _columns = {
        'name' : fields.char('Name', size=64),
        'notebook_sheet_lines' : fields.many2many('notebook.project', 'notebook_project_sheet_rel', 'notebook_project', 'notebook_project_id'),
        'notebook_project': fields.many2one('notebook.project', 'Project ID',domain=[('notebook_project_id','=',False)]),
        'project' : fields.many2one('project.project', 'Project'),
        'member' : fields.many2one('hr.employee', 'Members'),

        }

notebook_sheet()

编辑了我的答案,AnomA 先生。仍然不确定many2many,请检查一下

我是否也需要更改 onhange 事件?再次提前感谢

4

1 回答 1

0

首先请在 notebook.sheet 和 notebook.project 之间创建一个链接。现在这两个模型之间没有联系。在 notebook.project 中向 notebook.sheet 添加一个 many2one 关系,然后将 notebook_sheets_lines 中的关系 id 更改为该字段。否则将 notebook_sheet_lines 的关系类型更改为 many2many。然后更改将返回的 onchange_notebok_project(){'value':{'notebook_sheet_lines':LIST_OF_EMPOYEEIDS}} 也无需在 motebook_project 中添加 many2many。

于 2013-07-19T08:08:24.147 回答