Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
4 Răspunsuri
19103 Vizualizări

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

Imagine profil
Abandonează
Cel mai bun răspuns

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)

Imagine profil
Abandonează
Autor

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.

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.

Autor

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

Autor

I use the new API

try without context, as updated above

Autor

Perfect now Akhil, thanks :)

Cel mai bun răspuns

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})
Imagine profil
Abandonează
Autor Cel mai bun răspuns

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.

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
1
nov. 22
3984
1
oct. 21
2363
2
nov. 18
9369
1
mar. 15
2983
4
mar. 15
4587