Skip to Content
Odoo मेन्यू
  • Sign in
  • मुफ़्त में आज़माएं
  • ऐप्लिकेशन
    फ़ाइनेंस
    • अकाउंटिंग
    • इनवॉइसिंग
    • एक्सपेंस
    • स्प्रेडशीट (बीआई)
    • डॉक्यूमेंट्स
    • साइन
    सेल्स
    • सीआरएम
    • सेल्स
    • पीओएस शॉप
    • पीओएस रेस्टोरेंट
    • सब्सक्रिप्शन
    • रेंटल
    वेबसाइट
    • वेबसाइट बिल्डर
    • ई-कॉमर्स
    • ब्लॉग
    • फ़ोरम
    • लाइव चैट
    • ई-लर्निंग
    सप्लाई चेन
    • इन्वेंट्री
    • मैन्युफ़ैक्चरिंग
    • पीएलएम
    • परचेज़
    • मेंटेनेंस
    • क्वालिटी
    मानव संसाधन
    • कर्मचारी
    • रिक्रूटमेंट
    • टाइम ऑफ़
    • अप्रेज़ल
    • रेफ़रल
    • फ़्लीट
    मार्केटिंग
    • सोशल मार्केटिंग
    • ईमेल मार्केटिंग
    • एसएमएस मार्केटिंग
    • इवेंट
    • मार्केटिंग ऑटोमेशन
    • सर्वे
    सेवाएं
    • प्रोजेक्ट
    • टाइमशीट
    • फ़ील्ड सर्विस
    • हेल्पडेस्क
    • प्लानिंग
    • अपॉइंटमेंट
    प्रॉडक्टिविटी
    • डिस्कस
    • अप्रूवल
    • आईओटी
    • वीओआईपी
    • नॉलेज
    • WhatsApp
    तीसरे पक्ष के ऐप्लिकेशन Odoo स्टूडियो Odoo क्लाउड प्लेटफ़ॉर्म
  • इंडस्ट्री
    रीटेल
    • बुक स्टोर
    • क्लोदिंग स्टोर
    • फ़र्नीचर स्टोर
    • ग्रॉसरी स्टोर
    • हार्डवेयर स्टोर
    • टॉय स्टोर
    Food & Hospitality
    • बार और पब
    • रेस्टोरेंट
    • फ़ास्ट फ़ूड
    • Guest House
    • बेवरिज डिस्ट्रीब्यूटर
    • होटल
    रियल एस्टेट
    • Real Estate Agency
    • आर्किटेक्चर फ़र्म
    • कंसट्रक्शन
    • एस्टेट मैनेजमेंट
    • गार्ड्निंग
    • प्रॉपर्टी ओनर असोसिएशन
    कंसल्टिंग
    • अकाउंटिंग फ़र्म
    • Odoo पार्टनर
    • मार्केटिंग एजेंसी
    • लॉ फ़र्म
    • टैलेंट ऐक्विज़िशन
    • ऑडिट & सर्टिफ़िकेशन
    मैन्युफ़ैक्चरिंग
    • टेक्सटाइल
    • Metal
    • फ़र्नीचर
    • फ़ूड
    • Brewery
    • कॉर्पोरेट गिफ़्ट
    हेल्थ & फिटनेस
    • स्पोर्ट्स क्लब
    • आईवियर स्टोर
    • फिटनेस सेंटर
    • वेलनेस प्रैक्टिशनर
    • फॉर्मेसी
    • हेयर सैलून
    Trades
    • Handyman
    • आईटी हॉर्डवेयर और सपोर्ट
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    अन्य
    • Nonprofit Organization
    • एन्वायरमेंटल एजेंसी
    • बिलबोर्ड रेंटल
    • फ़ोटोग्राफी
    • बाइक लीजिंग
    • सॉफ़्टवेयर रीसेलर
    Browse all Industries
  • कम्यूनिटी
    सीखें
    • ट्यूटोरियल्स
    • दस्तावेज़
    • सर्टिफ़िकेशन
    • ट्रेनिंग
    • ब्लॉग
    • पॉडकास्ट
    शिक्षा को बढ़ावा दें
    • एजुकेशन प्रोग्राम
    • स्केल अप! बिजनेस गेम
    • Odoo के ऑफ़िस में आएं
    सॉफ़्टवेयर पाएं
    • डाउनलोड
    • वर्शन की तुलना करें
    • रिलीज़
    साथ मिलकर काम करें
    • Github
    • फ़ोरम
    • इवेंट
    • अनुवाद
    • पार्टनर बनें
    • Services for Partners
    • अपना अकाउंटिंग फ़र्म रजिस्टर करें
    सेवाएं पाएं
    • पार्टनर ढूंढें
    • अकाउंटेंट खोजें
    • सलाहकार की मदद लें
    • इम्प्लिमेंटेशन सेवाएं
    • कस्टमर रेफ़रेंस
    • सहायता
    • अपग्रेड
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    डेमो देखें
  • कीमत
  • सहायता

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

  • सीआरएम
  • e-Commerce
  • लेखांकन
  • इन्वेंटरी
  • PoS
  • प्रोजेक्ट
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
टैग (View all)
odoo accounting v14 pos v15
About this forum
Help

Copy One2many fields information to another model

Subscribe

Get notified when there's activity on this post

This question has been flagged
one2manyone2manyfieldv18CommunityEdition
2 Replies
1548 Views
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
Discard
Avatar
Moaaz Gafar
Best Answer

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
Discard
Avatar
D Enterprise
Best Answer

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
Discard
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Sign up
Related Posts Replies Views Activity
Custom functionality with One2many field
one2many one2many_list v18 CommunityEdition
Avatar
Avatar
1
मई 25
1873
Modify Pivot View
Pivot v18 CommunityEdition
Avatar
Avatar
1
जून 25
1741
Unable to send email notifications through write
notifications v18 CommunityEdition
Avatar
Avatar
1
जून 25
1794
Hide Records based on user group only in a particular view
views record_rule v18 CommunityEdition
Avatar
Avatar
Avatar
2
सित॰ 25
1473
Unable to pass context to an action
context server_action v18 CommunityEdition
Avatar
Avatar
1
जुल॰ 25
1621
कम्यूनिटी
  • ट्यूटोरियल्स
  • दस्तावेज़
  • फ़ोरम
ओपन सोर्स
  • डाउनलोड
  • Github
  • रनबॉट
  • अनुवाद
सेवाएं
  • Odoo.sh Hosting
  • सहायता
  • अपग्रेड
  • कस्टम डेवलपमेंट्स
  • शिक्षा
  • अकाउंटेंट खोजें
  • पार्टनर ढूंढें
  • पार्टनर बनें
हमारे बारे में
  • हमारी कंपनी
  • ब्रांड ऐसेट
  • संपर्क करें
  • नौकरियां
  • इवेंट
  • पॉडकास्ट
  • ब्लॉग
  • ग्राहक
  • लीगल • गोपनीयता
  • सुरक्षा
الْعَرَبيّة 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, बिज़नेस से जुड़े ऐप्लिकेशन का एक कलेक्शन है जो ओपन सोर्स पर आधारित है. इसमें आपकी कंपनी की हर ज़रूरत के लिए ऐप्लिकेशन हैं. जैसे, सीआरएम, ई-कॉमर्स, अकाउंटिंग, इन्वेंट्री, पॉइंट ऑफ़ सेल, प्रोजेक्ट मैनेजमेंट वगैरह.

Odoo की सबसे बड़ी खासियत है कि यह इस्तेमाल करने में बहुत आसान है और यह पूरी तरह से इंटिग्रेट किया हुआ है.

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