OpenERP : create new record ,one2many many2one relationship

ⅰ亾dé卋堺 提交于 2019-12-03 09:16:07

You have following errors:

  1. Your create call values should be a dictionary with field names as keys, not a list with tuples. The notation you are using is for writing/updating one2many fields.

  2. You are not creating 'class b' records, but creating 'class a' records instead (using self instead of a self.pool.get call)

So you should write

def save_b(self, cr, uid, ids, field_name, arg, context):
    b_obj = self.pool.get('class.b') # Fixes (#2)
    for record in self.browse(cr, uid, ids, context=context):
        num = 22
        for i in range(record.nombre):
            num += 1
            new_id = b_obj.create(cr, uid, {
                'ql': num,
                'id_classb': record.id
            }, context=context) # Fixes (#1)

Or as an alternative:

def save_b(self, cr, uid, ids, field_name, arg, context):
    for record in self.browse(cr, uid, ids, context=context):
        sub_lines = []
        num = 22
        for i in range(record.nombre):
            num += 1
            sub_lines.append( (0,0,{'q1': num}) )
            # Notice how we don't pass id_classb value here,
            # it is implicit when we write one2many field
        record.write({'Inventaire': sub_lines}, context=context)

Remark: In class b your link to class a is in column named 'id_classb'? The openerp-etiquette expects you to name them 'classa_id' or something similar.

Also, creating column names with capitals is frowned upon.

You can overwrite "Create" method of Class a & write the code to create records for class b in this create method.

i.e

def create(self, cr, uid, values, context=None):
    new_id = super(class_a, self).create(cr, uid, values, context)
    class_b_obj = self.pool.get('class.b')
    for i in values['nobr']:
        #  vals_b = {}
        # here create value list for class B 
         vals_b['ql'] = i
         vals_b['id_class_b'] = new_id 

        class_b_obj.create(cr,uid, vals_b , context=context)
    return new_id 

You can create one record with one2many relation like:

invoice_line_1 = {
   'name': 'line description 1',
   'price_unit': 100,
   'quantity': 1,
}

invoice_line_2 = {
   'name': 'line description 2',
   'price_unit': 200,
   'quantity': 1,
}

invoice = {
   'type': 'out_invoice',
   'comment': 'Invoice for example',
   'state': 'draft',
   'partner_id': 1,
   'account_id': 19,
   'invoice_line': [
       (0, 0, invoice_line_1),
       (0, 0, invoice_line_2)
   ]
}

invoice_id = self.pool.get('account.invoice').create(
        cr, uid, invoice, context=context)

return invoice_id
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!