沒加過濾條件的時候,效果如下,點擊下拉框,搜索更多出現所有模型下的模板:
改進方法(增加默認過濾條件,顯示指定模型下的內容):
class IrCloudReport(models.Model): _inherit = 'test.aaa' cr_template_no = fields.Char(string="模板號") # 唯一模板號 saas_domain_name = fields.Char(string="企業域名") class DecEXReportTempSelectWizard(models.TransientModel): _name = 'test.bbb' _description = 'Cloud Report Selection' model = fields.Char(string="模型名") cr_template_id = fields.Many2one(comodel_name="test.aaa", string="報表模板ID", copy=False, required=True, domain=lambda self: [('model', '=', self._context.get('res_model'))],) # 多對一字段 關聯報表模型
即:上方增加domain表達式:domain=lambda self: [('model', '=', self._context.get('res_model'))]
光寫表達式還沒有用,需要給當前模型cr_template_id字段所在的視圖中傳入上下文:
<record id="xxxxxxxxxx" model="ir.ui.view"> <field name="name">cus xxxxxxxx</field> <field name="model">test.xxxxxxx</field> <field name="inherit_id" ref="XXXXXXXXXXXXXXXXX"/> <field name="arch" type="xml"> <xpath expr="//header/button[@id='test.xxxxxxxxxx']" position="after"> <button name="%(test.xxxxxxxx)d" string="測試" type="action" class="btn-primary" context="{'res_model': '您自己需要過濾的模型名'}"/> </xpath> </field> </record>