-->

OpenERP : create new record ,one2many many2one rel

2020-06-06 07:41发布

问题:

I have created on2many field in class A and other field nombre (integer):

'Inventaire' : fields.one2many('class.b','id_classb'),

'nombre' : fields.integer('Nombre'),

In class b :

'id_classb' : fields.many2one('class.a', 'ID_classA'),

'ql' : fields.integer('QL'),

I want to create a function in class a that create records for object b according to the value of nombre field. for example if nombre =3 I should create 3 object of class b

here is my function:

 def save_b(self, cr, uid, ids, field_name, arg, context):
  a= self.browse(cr, uid, id)
  nbr=a.nombre
  num=22
  for i in range(nbr):
       num+=1
       self.create(cr, uid, [(0, 0,{'ql':num})])

I get these errors : TypeError:range()integer expected ,got NoneType ValueError: dictionary update sequence element #0 has length 3; 2 is required

can someone help me to improve my function?

回答1:

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.



回答2:

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 


回答3:

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