Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

insert new record into one2many field [Closed]

By
Hannes Smit
on 6/12/13, 9:37 AM 8,048 views

The Question has been closed

by
Sudhir Arya (SA)
on 06/12/2013 09:51:29

I thought it was possible in OE7 to write to an object with a one2many field.

For example: I want to add a student to a course:

course_osv.write(
    cr, uid, [course_id], {
        'student_ids': (0, 0, {'name': 'John', 'age': 12})
    }
)

Is this the right way to add a student to a course?

And yes, a course has a 'student_ids': fields.one2many(..) field.

Let's say that there are already 3 student on the course, and now I want to be adding 1 extra.

Take a look at 'https://doc.openerp.com/6.0/developer/2_5_Objects_Fields_Methods/methods/#osv.osv.osv.write' for the (0, 0, {}) notation.

5

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 6/12/13, 9:41 AM

Try this:

course_osv.write(cr, uid, [course_id], {
    'student_ids': [(0, 0, {'name': 'John', 'age': 12})]
})

For a one2many field, a lits of tuples is expected.

Here is the list of tuple that are accepted, with the corresponding semantics:

(0, 0,  { values }) #link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) #update the linked record with id = ID (write *values* on it)
(2, ID) #remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

Example:

[(0, 0, {'field_name':field_value_record1, ...}), (0, 0, {'field_name':field_value_record2, ...})]
0
Hannes Smit
On 6/12/13, 9:49 AM

Ah, I found the syntax error I made. Have a look at the following code:

course_osv.write(
    cr, uid, [course_id], {
        'student_ids': [(0, 0, {'name': 'John', 'age': 12}])
    }
)

yes, you didn't add tuple inside list.

Sudhir Arya (SA)
on 6/12/13, 9:50 AM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 6/12/13, 9:37 AM
Seen: 8048 times
Last updated: 5/12/16, 8:32 AM