0

我在res.partner模型中有一个选择字段,其中employmentstatus的选项是employed or unemployed。如果 或如果.我希望另一个字段employmenttype具有该属性。无论是否雇用合作伙伴,该字段现在都设置为 True(请参见此处的附图)。required=Trueemploymentstatus='employed'required=Falseemploymentstatus='unemployed'

这是我的代码:

from openerp.osv import osv, fields
from openerp import tools
class custom_fields_partner(osv.Model):
    _inherit = 'res.partner'
    _columns = {
        'employmentstatus' : fields.selection([
        ('employed','Employed'),
        ('unemployed','Unemployed')
        ],'Employment status', required=True, default='unemployed'),
        'employmenttype' : fields.selection([
        ('0','Public'),
        ('1','Private'),
        ('2','Mission')],'Nature of employment', required="fieldproperty"),
    }

    @api.one
    def fieldproperty(self):
        if self.employmentstatus == 'employed':
            return True
        else:
            return False
4

1 回答 1

1

所需的属性应该存储在数据库中,而不是动态计算。最好的选择是在客户端进行。如果您查看模型 ir.model.fields,您会注意到必填字段存储在数据库中,并不意味着要计算。

在您的 xml 中使用 attrs 属性。这是一个例子。

<field name="field_name" attrs="{'required':[('other_field','=','other_value')]}"/>

因此,在此示例中,field_name仅当字段other_field具有值时才需要调用的字段,other_value但您可以根据需要创建更复杂或更不复杂的域条件。

该字段other_field必须出现在您的视图中才能使其正常工作,因为评估发生在客户端。如果您需要包含一个用于评估的字段但不想显示它,您可以使其不可见。像这样。

<field name="other_field" invisible="1"/>
于 2017-02-08T17:10:18.153 回答