I want to get the value(Char) of the first line in one2many field into my main form.
Any advice?
Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:
I want to get the value(Char) of the first line in one2many field into my main form.
Any advice?
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'
how to make the condition when the data o2m field are empty? i dont know but i cant use this (if comment != False)
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
Cool.. i can use that,
Thankyou very much Prakash.
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.
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?
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
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?
self.field_name[0].name if self.field_name else ''
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
Using order line sequence field you find first order line. Order lines min(line.sequence) find and get value.
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")
updated the code, check it let me know if any issue.
Updated answer, please check
Create an account today to enjoy exclusive features and engage with our awesome community!
Sign up