Skip to Content
Odoo Menu
  • Sign in
  • 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
  • Accounting
  • Inventory
  • PoS
  • Project
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Tags (View all)
odoo accounting v14 pos v15
About this forum
Help

How to get the value(Char) of the first line in one2many field?

Subscribe

Get notified when there's activity on this post

This question has been flagged
13 Replies
9699 Views
Avatar
Boby

I want to get the value(Char) of the first line in one2many field into my main form.

Any advice?


0
Avatar
Discard
Avatar
Prakash
Best Answer

You can access one2many fields values using one2many filed name and dot notation. 

Example, In sale order main

rec =[ line.comment for line self.order_line]

The above rec variable get all lines comment data list.

To get first line.

rec =[ line.comment for line self.order_line]

line1 = rec[0]

To get particular line based on condition

rec =[ line.comment for line self.order_line if comment != False]

The above rec return list of sale order line id, based on above condition.

Edit based on your object,

@api.depends('certificate_of_competency_ids')
def _get_licence(self):
    first_line = self.env['hr.certificate.competency'].search([('id', 'in',     self.certificate_of_competency_ids.ids)], order='sequence, id', limit=1)
    if first_line and first_line.certificate_name_competency:
    self.licence = first_line.certificate_name_competency.name

class certificate_competency(models.Model):
    _name = 'hr.certificate.competency'
    _description = 'Certificate Competency'
    _order = 'coc_emp_id, sequence, id'
2
Avatar
Discard
Boby
Author

how to make the condition when the data o2m field are empty? i dont know but i cant use this (if comment != False)

Boby
Author

Hi, Praksesh

I've tried the code.. But there is an error:

first_line = self.certificate_of_competency_ids.filtered(lambda line: min(line.sequence))

TypeError: 'int' object is not iterable

Boby
Author

Cool.. i can use that,

Thankyou very much Prakash.

Avatar
Ray Carnes (ray)
Best Answer

You can do this directly within Odoo (without writing custom code) by making a related field.


Check out how we do this for the Next Activity Summary field on a Lead/Opportunity:






activity_ids is a one2many field.

2
Avatar
Discard
Boby
Author

can it show only one name of the first line in one2many field?

i mean, i make a field sequence widget="handle" so i can move it.. And if i move every data to the top of the line.. it show that data in my main form.

but i dont know if i make a related field i think all the data in o2m field will be showed and it will error?

Avatar
Odoo Mates
Best Answer

Hi,

You can get the value of the first line like this,

self.field_name[0] this will return you the id of the first line and if you need to access a particular field from it, self.field_name[0].name .


Thanks

1
Avatar
Discard
Boby
Author

i tried the code.. but its error when there is no line in the o2m data.. how to make the condition when the data is empty?

Odoo Mates

self.field_name[0].name if self.field_name else ''

Boby
Author

Thankyou Odoo Mates, it solve the empty data problem. But i have another problem, i add <field name="sequence" widget="handle"/> in my o2m field so i can drag and drop the lines. but not every data in the first line that showed up on my main form.

sometimes when i move another data to the first lines, data in the second lines showed up on my main form

Prakash

Using order line sequence field you find first order line. Order lines min(line.sequence) find and get value.

Boby
Author

Hi Prakash.

I'm sorry but i don't understand what do you mean. Can you help me with it

Here is my code:

class employee(models.Model):

_inherit = 'hr.employee'

licence = fields.Char(string="Licence", compute="_get_licence")

certificate_of_competency_ids = fields.One2many('hr.certificate.competency', 'coc_emp_id', String="Certificate of Competency")

(Now i'm using below function)

@api.depends('certificate_of_competency_ids')

def _get_licence(self):

self.licence = self.certificate_of_competency_ids[0].certificate_name_competency.name if self.certificate_of_competency_ids else ''

class certificate_competency(models.Model):

_name = 'hr.certificate.competency'

_description = 'Certificate Competency'

coc_emp_id = fields.Many2one('hr.employee', string="Employee Name")

sequence = fields.Integer(string='Sequence', default=10)

certificate_name_competency = fields.Many2one('hr.certificate.competency.name', string="Certificate Name")

Prakash

updated the code, check it let me know if any issue.

Prakash

Updated answer, please check

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
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