我正在尝试使用XML 文件中的 attrs来创建一个带有widget="radio"的选择字段。应用了 widget="radio" 时不需要选择字段。但是当我删除收音机小部件时,选择字段在创建新记录时在表单视图中获得所需的效果。
这是我在那里申请的选择字段:
<field name="installments_calculation" widget="radio" options="{'horizontal': true}" attrs="{'required': [('repayment_method', '=', 'salary deduction')]}"/>
Run Code Online (Sandbox Code Playgroud)
这是我的repayment_method:
repayment_method = fields.Selection([('cash/bank', 'Cash/Bank'), ('salary deduction', 'Salary Deduction')])
Run Code Online (Sandbox Code Playgroud)
我希望在 XML 文件中的条件下应用 required 属性时需要选择字段。对于带有 widget="radio" 的选择字段,这种行为是否正常,或者我做错了什么?如果这是正常的,我怎样才能获得widget="radio" 所需的选择字段?
您的代码应该可以正常工作,但如果此问题与小部件有关,请issue在 Odoo Github 中报告:
现在只是用来api.constrains获得相同的行为
# remember to depend on both fields
@api.constrains('installments_calculation','repayment_method')
def check_installments_calculation(self):
for rec in self:
if not rec.installments_calculation and rec.repayment_method == 'salary deduction':
raise exception.ValidationError(_('You message here'))
Run Code Online (Sandbox Code Playgroud)