Overslaan naar inhoud
Odoo Menu
  • Aanmelden
  • Probeer het gratis
  • Apps
    Financiën
    • Boekhouding
    • Facturatie
    • Onkosten
    • Spreadsheet (BI)
    • Documenten
    • Ondertekenen
    Verkoop
    • CRM
    • Verkoop
    • Kassasysteem winkel
    • Kassasysteem Restaurant
    • Abonnementen
    • Verhuur
    Websites
    • Websitebouwer
    • E-commerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Bevoorradingsketen
    • Voorraad
    • Productie
    • PLM
    • Inkoop
    • Onderhoud
    • Kwaliteit
    Personeelsbeheer
    • Werknemers
    • Werving & Selectie
    • Verlof
    • Evaluaties
    • Aanbevelingen
    • Wagenpark
    Marketing
    • Social media Marketing
    • E-mailmarketing
    • SMS Marketing
    • Evenementen
    • Marketingautomatisering
    • Enquêtes
    Diensten
    • Project
    • Urenstaten
    • Buitendienst
    • Helpdesk
    • Planning
    • Afspraken
    Productiviteit
    • Chat
    • Goedkeuringen
    • IoT
    • VoIP
    • Kennis
    • WhatsApp
    Apps van derden Odoo Studio Odoo Cloud Platform
  • Bedrijfstakken
    Detailhandel
    • Boekhandel
    • kledingwinkel
    • Meubelzaak
    • Supermarkt
    • Bouwmarkt
    • Speelgoedwinkel
    Food & Hospitality
    • Bar en Pub
    • Restaurant
    • Fastfood
    • Gastenverblijf
    • Drankenhandelaar
    • Hotel
    Vastgoed
    • Makelaarskantoor
    • Architectenbureau
    • Bouw
    • Vastgoedbeheer
    • Tuinieren
    • Vereniging van eigenaren
    Consulting
    • Accountantskantoor
    • Odoo Partner
    • Marketingbureau
    • Advocatenkantoor
    • Talentenwerving
    • Audit & Certificering
    Productie
    • Textiel
    • Metaal
    • Meubels
    • Eten
    • Brewery
    • Relatiegeschenken
    Gezondheid & Fitness
    • Sportclub
    • Opticien
    • Fitnesscentrum
    • Wellness-medewerkers
    • Apotheek
    • Kapper
    Trades
    • Klusjesman
    • IT-hardware & support
    • Zonne-energiesystemen
    • Schoenmaker
    • Schoonmaakdiensten
    • HVAC-diensten
    Andere
    • Non-profitorganisatie
    • Milieuagentschap
    • Verhuur van Billboards
    • Fotograaf
    • Fietsleasing
    • Softwareverkoper
    Browse all Industries
  • Community
    Leren
    • Tutorials
    • Documentatie
    • Certificeringen
    • Training
    • Blog
    • Podcast
    Versterk het onderwijs
    • Onderwijs- programma
    • Scale Up! Business Game
    • Bezoek Odoo
    Download de Software
    • Downloaden
    • Vergelijk edities
    • Releases
    Werk samen
    • Github
    • Forum
    • Evenementen
    • Vertalingen
    • Word een Partner
    • Services for Partners
    • Registreer je accountantskantoor
    Diensten
    • Vind een partner
    • Vind een boekhouder
    • Een adviseur ontmoeten
    • Implementatiediensten
    • Klantreferenties
    • Ondersteuning
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Vraag een demo aan
  • Prijzen
  • Help

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Boekhouding
  • Voorraad
  • PoS
  • Project
  • MRP
All apps
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Alle posts Personen Badges
Labels (Bekijk alle)
odoo accounting v14 pos v15
Over dit forum
Help

Copy One2many fields information to another model

Inschrijven

Ontvang een bericht wanneer er activiteit is op deze post

Deze vraag is gerapporteerd
one2manyone2manyfieldv18CommunityEdition
2 Antwoorden
1595 Weergaven
Avatar
Shreya Doodipala

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

0
Avatar
Annuleer
Avatar
Moaaz Gafar
Beste antwoord

Hi 

Store Secondary Sales Data Directly 

Instead of trying to replicate the one2many relationship, store the secondary sales data as computed/stored fields or JSON in your custom.crm.lead.view model:

class CustomCrmLeadView(models.Model):
    _name = 'custom.crm.lead.view'
    _description = 'CRM Lead View Extension'
   
    lead_id = fields.Many2one('crm.lead', string='Lead', required=True, ondelete='cascade')
   
    # Store secondary sales data as JSON or separate fields
    secondary_sales_data = fields.Text('Secondary Sales Data', help='JSON data of secondary salespeople')
    secondary_sales_count = fields.Integer('Secondary Sales Count', compute='_compute_secondary_sales', store=True)
    secondary_sales_names = fields.Char('Secondary Salespeople Names', compute='_compute_secondary_sales', store=True)
    total_deal_share = fields.Float('Total Deal Share', compute='_compute_secondary_sales', store=True)
   
    @api.depends('lead_id.sales_secondary_ids')
    def _compute_secondary_sales(self):
        for record in self:
            if record.lead_id and record.lead_id.sales_secondary_ids:
                secondary_data = []
                names = []
                total_share = 0
               
                for secondary in record.lead_id.sales_secondary_ids:
                    data = {
                        'user_id': secondary.user_id.id,
                        'user_name': secondary.user_id.name,
                        'team_id': secondary.team_id.id if secondary.team_id else False,
                        'team_name': secondary.team_id.name if secondary.team_id else '',
                        'deal_share': secondary.deal_share,
                    }
                    secondary_data.append(data)
                    names.append(secondary.user_id.name)
                    total_share += secondary.deal_share or 0
               
                record.secondary_sales_data = json.dumps(secondary_data)
                record.secondary_sales_count = len(secondary_data)
                record.secondary_sales_names = ', '.join(names)
                record.total_deal_share = total_share
            else:
                record.secondary_sales_data = '[]'
                record.secondary_sales_count = 0
                record.secondary_sales_names = ''
                record.total_deal_share = 0

Best regards, 

E: m.gafar2024@gmail.com

0
Avatar
Annuleer
Avatar
D Enterprise
Beste antwoord

Hii,

Define a mirror model:
class CustomCrmLeadSecondaryView(models.Model):

    _name = 'custom.crm.secondary.view'

    _description = 'Synced Secondary Sales Data for Leads'


    lead_id = fields.Many2one('crm.lead', string='Lead', ondelete='cascade')

    view_id = fields.Many2one('custom.crm.lead.view', string='Master View', ondelete='cascade')


    user_id = fields.Many2one('res.users', string='Salesperson')

    team_id = fields.Many2one('crm.team', string='Sales Team')

    team_leader_id = fields.Many2one('res.users', string='Team Leader')

    deal_share = fields.Monetary('Share of Deal', currency_field='currency_id')

    currency_id = fields.Many2one('res.currency', string='Currency')


Update _update_master_view to also sync the secondary salespeople

Inherit crm.lead, and update create and write:
class CrmLead(models.Model):

    _inherit = 'crm.lead'


    def _update_master_view(self):

        for lead in self:

            extension = self.env['custom.crm.lead.view'].search([('lead_id', '=', secondary.currency_id.id,

                })


    @api.model

    def create(self, vals):

        lead = super().create(vals)

        lead._update_master_view()

        return lead


    def write(self, vals):

        result = super().write(vals)

        self._update_master_view()

        return result

In custom.crm.lead.view, add a One2many to show them
class CustomCrmLeadView(models.Model):

    _name = 'custom.crm.lead.view'


    lead_id = fields.Many2one('crm.lead', string='Lead')

    secondary_sales_view_ids = fields.One2many('custom.crm.secondary.view', 'view_id', string='Secondary Sales')


0
Avatar
Annuleer
Geniet je van het gesprek? Blijf niet alleen lezen, doe ook mee!

Maak vandaag nog een account aan om te profiteren van exclusieve functies en deel uit te maken van onze geweldige community!

Aanmelden
Gerelateerde posts Antwoorden Weergaven Activiteit
Custom functionality with One2many field
one2many one2many_list v18 CommunityEdition
Avatar
Avatar
1
mei 25
1979
Modify Pivot View
Pivot v18 CommunityEdition
Avatar
Avatar
1
jun. 25
1823
Unable to send email notifications through write
notifications v18 CommunityEdition
Avatar
Avatar
1
jun. 25
1885
Hide Records based on user group only in a particular view
views record_rule v18 CommunityEdition
Avatar
Avatar
Avatar
2
sep. 25
1517
Unable to pass context to an action
context server_action v18 CommunityEdition
Avatar
Avatar
1
jul. 25
1698
Community
  • Tutorials
  • Documentatie
  • Forum
Open Source
  • Downloaden
  • Github
  • Runbot
  • Vertalingen
Diensten
  • Odoo.sh Hosting
  • Ondersteuning
  • Upgrade
  • Gepersonaliseerde ontwikkelingen
  • Onderwijs
  • Vind een boekhouder
  • Vind een partner
  • Word een Partner
Over ons
  • Ons bedrijf
  • Merkelementen
  • Neem contact met ons op
  • Vacatures
  • Evenementen
  • Podcast
  • Blog
  • Klanten
  • Juridisch • Privacy
  • Beveiliging
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo is een suite van open source zakelijke apps die aan al je bedrijfsbehoeften voldoet: CRM, E-commerce, boekhouding, inventaris, kassasysteem, projectbeheer, enz.

Odoo's unieke waardepropositie is om tegelijkertijd zeer gebruiksvriendelijk en volledig geïntegreerd te zijn.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now