从所选项目中获取任务

时间:2015-04-03 18:40:06

标签: openerp

我正在尝试实现一个选择项目的many2one选择字段。如果您选择了一个项目,那么还有另一个很多的字段,您可以从中选择任务。这些任务都需要来自所选项目。

目前我得到了这个(注意我无法测试它,因为我一直收到XML错误):

class purchase_order(osv.osv):
_inherit = 'purchase.order'

def get_task(self, cr, uid, ids, project_id, context=None):
    task_obj = self.pool.get('project.task')
    for task in task_obj.browse(cr, uid, ids, context):
        task_ids = task_obj.search(cr, uid, [(task.project_id.id, '=', project_id)])
        ids_cus = []
        for cus in task_obj.browse(cr, uid, task_ids, context):
            if cus.project.id.id not in ids_cus:
                ids_cus.append(cus.project_id.id)
        self.write(cr, uid, ids, {'state_readonly': 'listed', 'task_ids': [(6, 0, ids_cus)]})
    return True

_columns = {
    'project_id': fields.many2one('project.project', 'Project'),
    'task_id': fields.selection(get_task, 'Select task'),
}

我的XML看起来像这样:

   <record id="purchase_order_form" model="ir.ui.view">
        <field name="name">purchase.order.form</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form"/>
        <field name="arch" type="xml">
            <field name="origin" position="after">
                <field name="project_id" on_change="_get_task(project_id)"/>
                <field name="task_id" selection="widget"/>
            </field>
        </field>
    </record>

我在这里做错了什么?可能还有另一种方式吗?

2 个答案:

答案 0 :(得分:1)

根据您的案例中的项目,无需编写方法来过滤任务,只需更改以下内容即可。

class purchase_order(osv.osv):
_inherit = 'purchase.order'

_columns = {
    'project_id': fields.many2one('project.project', 'Project'),
    'task_id': fields.many2one('project.task', 'Tasks'),
}

并更改您的xml,

<record id="purchase_order_form" model="ir.ui.view">
        <field name="name">purchase.order.form</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form"/>
        <field name="arch" type="xml">
            <field name="origin" position="after">
                <field name="project_id" />
                <field name="task_id" domain="[('project_id','=',project_id.id)]" widget="selection" />
            </field>
        </field>
    </record>

在您现有的代码中,您犯了一个小错误,

selection="widget"在xml中无效,您应该写widget="selection"

答案 1 :(得分:1)

这解决了我的问题,我为链接字段添加了fields.related:

    _columns = {
    'task_id': fields.related('project_id', 'tasks', type='many2one', relation='project.task', store=True,
                              string='Task')
}

然后调整我的XML(如@Empiro Technologies所说):

    <record id="purchase_order_form" model="ir.ui.view">
        <field name="name">purchase.order.form</field>
        <field name="model">purchase.order</field>
        <field name="inherit_id" ref="purchase.purchase_order_form"/>
        <field name="arch" type="xml">
            <field name="origin" position="after">
                <field name="project_id"/>
                <field name="task_id" domain="[('project_id','=', project_id)]"/>
            </field>
        </field>
    </record>