In my custom (inherited) crm.lead model, I have the following one2many field for secondary sales people:
sales_secondary_ids = fields.One2many('custom.crm.sales.secondary', 'lead_id', string='Secondary Salespeople', index=True, tracking=True)
The custom.crm.sales.secondary model has the following fields:
lead_id = fields.Many2one('crm.lead', string='Opportunity', ondelete='cascade')
user_id = fields.Many2one('res.users', string='Salesperson', required=True, check_company=True, tracking=True, domain=lambda self: self._get_sales_users_domain())
team_id = fields.Many2one('crm.team', string='Sales Team', check_company=True, tracking=True, related='user_id.sale_team_id', ondelete="set null", readonly=True, store=True)
team_leader_id = fields.Many2one('res.users', string='Team Leader', related='team_id.user_id', store=True, readonly=True)
currency_id = fields.Many2one('res.currency', string='Currency', readonly=True, required=True, default=lambda self: self.env.company.currency_id)
deal_share = fields.Monetary('Share of Deal', currency_field='currency_id', tracking=True)
among other fields and functions.
How do I copy the one2many fields' information from crm.lead to another model for further operations and views? I want to auto update this model whenever a new lead is created or updated in crm.lead using
def _update_master_view(self):
for lead in self:
extension = self.env['custom.crm.lead.view'].search([('lead_id', '=', lead.id)], limit=1)
if extension:
# Update existing record (if you want to sync additional data, you can add it here)
extension.write({})
else:
# Create new extension record
self.env['custom.crm.lead.view'].create({
'lead_id': lead.id,
extension.write({})
})
in the create and write methods.
I can't create a one2many field in the custom.crm.lead.view model as it requires the custom.crm.sales.secondary to have a many2one relation on this model.
Odoo v18 Community Edition