0
preload = fields.Boolean(related='project_id.preload', string='Preload Templates')

part_template_ids = fields.Many2many(
        'project.part.template', string='Part Templates', required=True,
default='_default_part_template_ids')

  def _default_part_template_ids(self):
        domain = [('case_default', '=', True)]
        return self.env['project.part.template'].search(domain)

my goal is to change part_template_ids default based on preload field. If preload is True then part_template_ids default='_default_part_template_ids' if preload is false then default for part_template_ids is false too. how can i do this?

Chaban33
  • 1,362
  • 11
  • 38
  • If you check `self` inside the default methods you've made, you'll see that it's empty (as the record hasn't been created yet), so you can't make a default value depend on another value through that way. I'd use `onchange` method, each time `project_id` (or `preload`) changes, fill in `part_template_ids` with the data you want. – forvas Apr 24 '18 at 08:05
  • 1
    @forvas setting a default on `preload` and writing an onchange method depending on it (with Chaban33 logic) should be enough. That worked very well in older Odoo versions, but i don't know if the new API is working as well. – CZoellner Apr 24 '18 at 11:12
  • @CZoellner I think what you wrote works now too, it would be the best solution as far as I know. – forvas Apr 24 '18 at 11:47

1 Answers1

1

At first you have to add a default value to preload

preload = fields.Boolean(
    related='project_id.preload', string='Preload Templates',
    default=False)

That will trigger onchange events, even on initial creation. You can use that to fill default values for other fields.

@api.onchange('preload')
@api.multi
def onchange_preload(self):
    """ Preloads part templates if set to true"""
    if self.preload:
        domain = [('case_default', '=', True)]
        self.part_template_ids = self.env['project.part.template'].search(domain)
    else:
        self.part_template_ids = self.env['project.part.template']
CZoellner
  • 13,553
  • 3
  • 25
  • 38