Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Apps
    Finance
    • Accounting
    • Invoicing
    • Expenses
    • Spreadsheet (BI)
    • Documents
    • Sign
    Sales
    • CRM
    • Sales
    • POS Shop
    • POS Restaurant
    • Subscriptions
    • Rental
    Websites
    • Website Builder
    • eCommerce
    • Blog
    • Forum
    • Live Chat
    • eLearning
    Supply Chain
    • Inventory
    • Manufacturing
    • PLM
    • Purchase
    • Maintenance
    • Quality
    Human Resources
    • Employees
    • Recruitment
    • Time Off
    • Appraisals
    • Referrals
    • Fleet
    Marketing
    • Social Marketing
    • Email Marketing
    • SMS Marketing
    • Events
    • Marketing Automation
    • Surveys
    Services
    • Project
    • Timesheets
    • Field Service
    • Helpdesk
    • Planning
    • Appointments
    Productivity
    • Discuss
    • Approvals
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industries
    Retail
    • Book Store
    • Clothing Store
    • Furniture Store
    • Grocery Store
    • Hardware Store
    • Toy Store
    Food & Hospitality
    • Bar and Pub
    • Restaurant
    • Fast Food
    • Guest House
    • Beverage Distributor
    • Hotel
    Real Estate
    • Real Estate Agency
    • Architecture Firm
    • Construction
    • Estate Management
    • Gardening
    • Property Owner Association
    Consulting
    • Accounting Firm
    • Odoo Partner
    • Marketing Agency
    • Law firm
    • Talent Acquisition
    • Audit & Certification
    Manufacturing
    • Textile
    • Metal
    • Furnitures
    • Food
    • Brewery
    • Corporate Gifts
    Health & Fitness
    • Sports Club
    • Eyewear Store
    • Fitness Center
    • Wellness Practitioners
    • Pharmacy
    • Hair Salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Solar Energy Systems
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Others
    • Nonprofit Organization
    • Environmental Agency
    • Billboard Rental
    • Photography
    • Bike Leasing
    • Software Reseller
    Browse all Industries
  • Community
    Learn
    • Tutorials
    • Documentation
    • Certifications
    • Training
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Download
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Events
    • Translations
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Customer References
    • Support
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Pricing
  • Help

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

How can I get the parent value in the creation of a child? In Odoo 9

Naroči se

Get notified when there's activity on this post

This question has been flagged
defaultodooodoo9
1 Odgovori
29224 Prikazi
Avatar
Nacho

I want to create a new register, but I need to put the value of a parent field as a default value of a child field.
I know that you can send parent values through context from XML and whenever a new form is opened, "default_get" of that object will be executed.

Example, [In XML view]:
<field name="one2many_ids" context="{'variable1' : name, 'variable2' : active_id, 'default_column1' : 'samplevalue'}">
Note: [There are 3 ways of setting default value]
 1. Variable1: you can pass parent value, which you can retrieve in default_get method.. 2. Variable2: "active_id" is a keyvalue, which provides ID of its immediate parent object, so in default_get you can browse and fetch the parent record , provided parent record must be saved in order to get it... 3. default_column: straight away setting default values for a column.

My problem now is:
How can I access to those values in the default_get method?

I have tried this:

@api.model
def default_get(self, vals):
    context = dict(self.env.context)
    variable1 = context.get('variable1', False)
    print ' variable1'

But this has not worked.

0
Avatar
Opusti
Avatar
Adrien
Best Answer

Hi,


You don't need to use default_get.

In your context in xml in the one2manyfield :
context = {'parent_id': active_id, 'parent_model': 'your.parent_model'}

If you want to get the parent obj in the child object to set some default fields of the child, you could try smth like this in the child class (replace with your values, of course) :

def _get_some_default(self):

     parent_id = self.env.context.get('parent_id') 

     parent_model = self.env.context.get('parent_model')       

     if parent_id and parent_model:

         parent_obj = self.env[parent_model].browse(parent_id)

         # now you have the parent obj to do what you want

         default_value = ... use the parent

         return default_value


some_field = fields.Char(default=_get_some_default)



1
Avatar
Opusti
Nacho
Avtor

Ok, I pass this value in the context, but when do I pick it? In what method?

OdooBot
Why do you need to pick it?
It should be set by default when you open the form by clicking on the field.

Le jeu. 7 juin 2018 18:22, Nacho <nacho@baintex.com> a écrit :

Ok, I pass this value in the context, but when do I pick it? In what method?

--
Nacho

Sent by Odoo S.A. using Odoo.

Nacho
Avtor

But I need to pick the value to access to some fields of the parent and so stuff for the default value of some field of the child.

Adrien

I edited my answer

Nacho
Avtor

Okay, I have added a print after active_id and active_model and both values are None.

If I print the context, I have only this:

context= {u'lang': u'es_ES', u'tz': u'Europe/Madrid', u'uid': 1}

OdooBot
Can you try passing them from the xml ?

In your context in xml in the one2manyfield :
context = {'parent_id': active_id, 'parent_model': 'your.parent_model'}

=> and use this in the default function instead of active_id/active_model

2018-06-08 11:01 GMT+02:00 Nacho <nacho@baintex.com>:

Okay, I have added a print after active_id and active_model and both values are None.

If I print the context, I have only this:

context= {u'lang': u'es_ES', u'tz': u'Europe/Madrid', u'uid': 1}

--
Nacho

Sent by Odoo S.A. using Odoo.




--
Adrien Combourieu
00 33 6 29 85 94 59
adrien.combourieu@gmail.com
Nacho
Avtor

That worked :)

I realised that I was passing the context through a child field, and that is wrong. The correct way is to pass the context through the One2many field.

Adrien

OK I edited the answer. Can you upvote it ?

Sehrish

Get parent form value in one2many form view or pop up form view, refer below link hope this may helps you

http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html

Nacho
Avtor

Thank you both.

@Adrien I don't know why I cannot vote up your answer. I tried with different browser, and it seems that its a bug with the web. But I have voted it as a solution.

Enjoying the discussion? Don't just read, join in!

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

Prijavi
Related Posts Odgovori Prikazi Aktivnost
How to bring products from opportunity to quotation
odoo odoo9
Avatar
0
okt. 23
3538
Preview pdf,docs or other attachment file Solved
odoo odoo9
Avatar
Avatar
Avatar
Avatar
Avatar
4
dec. 21
34627
odoo 9 show user popups Solved
odoo odoo9
Avatar
Avatar
2
dec. 20
9669
installable , auto_install and application in __openerp__.py file
odoo odoo9
Avatar
Avatar
2
avg. 18
8388
How to override javascript function in main mail module to my module
odoo odoo9
Avatar
Avatar
1
jul. 17
5309
Community
  • Tutorials
  • Documentation
  • Forum
Open Source
  • Download
  • Github
  • Runbot
  • Translations
Services
  • Odoo.sh Hosting
  • Support
  • Upgrade
  • Custom Developments
  • Education
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Brand Assets
  • Contact us
  • Jobs
  • Events
  • Podcast
  • Blog
  • Customers
  • Legal • Privacy
  • Security
الْعَرَبيّة 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 a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

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