如何在 many2one 中搜索其他字段?
how to search other field in many2one?
python代码:
_name = 'res.partner.table2'
customer_id = fields.Many2one('res.partner', required=True ,string="Customer ID")
XML:
<field name="arch" type="xml">
<form string="Reward Points">
<field name="customer_id"/> //how can i search by res.partner customer_id in here
</form>
</field>
我需要通过其他字段搜索 "name"。怎么可能?
我们可以通过以下方式实现:
<field name="customer_id" context="{'my_custom_search': True}"/>
我们添加上下文以确保功能将影响特定领域而不干扰现有功能。
class res_partner(models.Model):
_inherit = 'res.partner'
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
if not args:
args = []
if not context:
context = {}
if context.has_key('my_custom_search'):
domain = [('field_name', 'operator', value)]
ids = self.search(domain)
else:
return super(res_partner, self).name_search(cr, user, name, args=args, operator='ilike', context=context, limit=limit)
return self.name_get(cr, uid, ids, context)
例如,如果您想在 hr.employee
中按新字段 (emp_code
) 进行搜索,我会建议如下:
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
domain = []
if name:
domain = ['|', ('emp_code', 'ilike', name), ('name', operator, name)]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&', '!'] + domain[1:]
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
expression.NEGATIVE_TERM_OPERATORS
将检查否定运算符,如 !=
。
这段代码的“灵感”来源于 Odoo 的 account/models/account_account.py
here。
旁注:Odoo 的所有社区版本都继承自 _name_search
而不是 name_search
python代码:
_name = 'res.partner.table2'
customer_id = fields.Many2one('res.partner', required=True ,string="Customer ID")
XML:
<field name="arch" type="xml">
<form string="Reward Points">
<field name="customer_id"/> //how can i search by res.partner customer_id in here
</form>
</field>
我需要通过其他字段搜索 "name"。怎么可能?
我们可以通过以下方式实现:
<field name="customer_id" context="{'my_custom_search': True}"/>
我们添加上下文以确保功能将影响特定领域而不干扰现有功能。
class res_partner(models.Model):
_inherit = 'res.partner'
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
if not args:
args = []
if not context:
context = {}
if context.has_key('my_custom_search'):
domain = [('field_name', 'operator', value)]
ids = self.search(domain)
else:
return super(res_partner, self).name_search(cr, user, name, args=args, operator='ilike', context=context, limit=limit)
return self.name_get(cr, uid, ids, context)
例如,如果您想在 hr.employee
中按新字段 (emp_code
) 进行搜索,我会建议如下:
@api.model
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None):
args = args or []
domain = []
if name:
domain = ['|', ('emp_code', 'ilike', name), ('name', operator, name)]
if operator in expression.NEGATIVE_TERM_OPERATORS:
domain = ['&', '!'] + domain[1:]
return self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid)
expression.NEGATIVE_TERM_OPERATORS
将检查否定运算符,如 !=
。
这段代码的“灵感”来源于 Odoo 的 account/models/account_account.py
here。
旁注:Odoo 的所有社区版本都继承自 _name_search
而不是 name_search