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”。怎么可能?
答案 0 :(得分:1)
我们可以通过以下方式实现:
<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)
答案 1 :(得分:0)
例如,如果您想按 emp_code
中的新字段 (hr.employee
) 进行搜索,我会建议如下:
@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