背景:
一个many2one
字段,在界面上下拉选值的时候,有一个打开弹窗选择的页面,有时候需要对这个数据排序,方便用户使用。
思路:
需要搞清楚的一点就是:点击这个many2one
字段,默认会发送一个search_read
请求,我们需要做的就是在这个过程中传递一个消息告诉odoo到这个位置了,需要排序了。当然,这个消息的发送,就可以用conext来传递。
实现:
在xml中对应的字段中设置conetxt
<field name="budget_item_id" context="{'budget_item_order': True}"/>
这里给预算项目字段一个上下文:budget_item_order
为True
然后在对应模型的py文件中编写search_read方法:
@api.model def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): # if not domain: # domain = [] # domain += self._get_domains() # if self.env.context.get('budget_control_rule', False): # order = 'code' if self.env.context.get('budget_item_order', False): order = 'order' return super(KthrpBudgetBudgetItem, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order) 注: 不知道只写着一个方法会不会生效,下面有全部的方法,不生效尝试下面的5个方法同时写 def _get_domains(self): domain = [] if self.env.context.get('budget_control_rule', False): domain += [('company_id', 'in', (False, self.env.user.company_id.id)), ('business_group_id', '=', self.env.user.company_id.business_group_id.id)] budget_category_company = self.env.context.get('budget_category_company', False) if budget_category_company: company_id = budget_category_company[1] business_group_id = budget_category_company[0] if company_id: domain += [('company_id', 'in', (company_id, False))] else: domain += [('business_group_id', '=', business_group_id)] select_depend_type_id = self.env.context.get('select_depend_type_id',False) if select_depend_type_id: budget_item_ids = self.env['kthrp.budget.budget.sheet.document.type'].browse(select_depend_type_id).budget_item_ids.ids if budget_item_ids: domain += [('id','in', budget_item_ids)] return domain @api.model def name_search(self, name, args=None, operator='ilike', limit=100): args = args or [] domain = self._get_domains() return super(KthrpBudgetBudgetItem, self).name_search(name, args=args + domain, operator=operator, limit=limit) ######################################################################################## @api.model def search_read(self, domain=None, fields=None, offset=0, limit=None, order=None): if not domain: domain = [] domain += self._get_domains() if self.env.context.get('budget_control_rule', False): order = 'code' elif self.env.context.get('budget_item_order', False): order = 'order' return super(KthrpBudgetBudgetItem, self).search_read(domain=domain, fields=fields, offset=offset, limit=limit, order=order) ######################################################################################### @api.model def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): if not domain: domain = [] domain += self._get_domains() return super(KthrpBudgetBudgetItem, self).read_group(domain=domain, fields=fields, groupby=groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy) @api.model def search_count(self, args): domain = args or [] domain += self._get_domains() return super(KthrpBudgetBudgetItem, self).search_count(domain)