Skip to Content
Menu
This question has been flagged
13 Replies
8655 Views

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

Any advice?


Avatar
Discard
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'
Avatar
Discard
Author

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

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

Author

Cool.. i can use that,

Thankyou very much Prakash.

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.

Avatar
Discard
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?

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

Avatar
Discard
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?

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

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

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

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

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

Updated answer, please check