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

By
Sebastian782
on 10/6/15, 7:07 AM 729 views

Hello,

I have 2 models A and B.

When i update field_1 on A model i execute a function that should add a new record into B model.

How can i do this?

I read that we can execute SQL request (example belaw) but maybe there is an other way more clean than do an INSERT to add new record?

query = """ SELECT event_id, state, sum(nb_register) 
            FROM event_registration
            WHERE event_id IN %s AND state IN ('draft', 'open', 'done')
            GROUP BY event_id, state """
self._cr.execute(query, (tuple(self.ids),))

Thanks,
Sebastian

1

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 10/6/15, 7:44 AM

You can override the write function of model_A for that, so that when you are saving the record, it create a new record on model_B.

For eg, try the following, if you want to use old api in your v8 module:

from openerp.osv import fields, osv
class class_A(osv.osv):
_name = 'model.A'
_columns = {
'field_A': fields.many2one('res.partner', string="Supplier", required=True),

}

def write(self, cr, uid, ids, vals, context=None):

model_B_obj = self.pool.get('model_B')
model_B_obj.create(cr, uid, ids, {'product_id':ids, 'date':vals['status_date'], 'status':vals['status']}, context)

return super(class_A, self).write(cr, uid, ids, vals, context)

Using new api, you may try like this:

class class_A(models.Model):
_name = 'model.A'

field_A = fields.Many2one('res.partner', string="Supplier", required=True)


 
@api.multi
def write(self, vals):

model_B_obj = self.env['model_B'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status})

return super(class_A, self).write(vals)

Hi, thanks. I don't know why, i am not able to make it work. When i change my select field, it seems that it create a record, with the default values of model.B. First, I want to create record in model.B when i save model.A record, so i think the onchange is not good? It seem that it create a record when Second, i want to create the model.B with my values status_date and status of model.A Here below what i wrote: ------------------------------- @api.onchange('status') def on_change_status(self): self.env['order.history'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status}) What i am doing wrong? Thanks.

Sebastian782
on 10/12/15, 5:43 AM

Hi Sebastian, I have updated the answer. You don't need to use onchange for that, just need to override the write function of model_A. Try like above.

Akhil P Sivan
on 10/12/15, 8:09 AM

Hi Akhil, thanks for your help. I have this error : TypeError: write() takes at least 2 arguments (2 given)

Sebastian782
on 10/13/15, 12:27 AM

I use the new API

Sebastian782
on 10/13/15, 12:38 AM

try without context, as updated above

Akhil P Sivan
on 10/13/15, 1:06 AM

Perfect now Akhil, thanks :)

Sebastian782
on 10/13/15, 6:09 AM
0
james_p
On 10/6/15, 7:31 AM

You can achieve it without firing SQL request. Just add the below method in model A. You can read more here: https://www.odoo.com/documentation/8.0/reference/orm.html Hope that helps.

@api.onchange('field_1')
def create_new_record(self):
self.env['model_b'].create({'field_in_model_b': value})
0
Sebastian782
On 10/12/15, 5:45 AM

Hi, thanks.

I don't know why, i am not able to make it work.

When i change my select field, it seems that it create a record, with the default values of model.B.

First, I want to create record in model.B when i save model.A record, so i think the onchange is not good? It seem that it create a record when

Second, i want to create the model.B with my values status_date and status of model.A

Here below what i wrote:

-------------------------------

@api.onchange('status')

def on_change_status(self):

self.env['order.history'].create({'product_id':self.id, 'date':self.status_date, 'status':self.status})

What i am doing wrong?

Thanks.

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

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

Question tools

1 follower(s)

Stats

Asked: 10/6/15, 7:07 AM
Seen: 729 times
Last updated: 10/14/15, 12:37 AM