从选定的项目中获取任务

Get tasks from a selected project

我正在尝试实施一个 many2one selection 场,您 select 一个项目来自该场。如果您已经 select 编辑了一个项目,那么还有另一个 many2one 字段,您可以从中 select 任务。这些任务都需要来自 selected 项目。

目前我得到了这个(请注意我无法测试它,因为我一直收到 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>

这里的XML我做错了什么?还有其他方法吗?

无需编写根据项目过滤任务的方法,只需更改以下几项即可。

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"

这解决了我的问题,我为链式字段添加了 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>