4

Let say I have such classes:

class First(orm.Model):
    _name = 'first.class'
    _columns = {
        'partner_id': fields.many2one('res.partner', 'Partner'),
        'res_ids': fields.one2many('second.class', 'first_id', 'Resources'),   
    }
class Second(orm.Model):
    _name = 'second.class'
    _columns = {
        'partner_id': fields.many2one('res.partner', 'Partner'),
        'first_id': fields.many2one('first.class', 'First'),
    }

Now I want to write a method, when it is called, it would create First class record taking values from Second class. But I don't understand how I should pass values in one2many field while creating First class.

For example let say I call create like this (this method is inside Second class):

def some_method(cr, uid, ids, context=None):
    vals = {}
    for obj in self.browse(cr, uid, ids):
        #many2one value is easy. I just link one with another like this
        vals['partner_id'] = obj.partner_id and obj.partner_id.id or False
        #Now the question how to insert values for `res_ids`?
        #`res_ids` should take `id` from `second.class`, but how?..
        vals['res_ids'] = ??
        self.pool.get('first.class').create(cr, uid, vals)
    return True

There is a documentation about inserting one2many values here: https://doc.openerp.com/v6.0/developer/2_5_Objects_Fields_Methods/methods.html/#osv.osv.osv.write

But that is only for write method.

Andrius
  • 19,658
  • 37
  • 143
  • 243

3 Answers3

5

Try this Values, If the second.class obj has partner than one2many is created.

obj = self.browse(cr, uid, ids)[0]

if obj.partner_id:
    vals{
    'partner_id': obj.partner_id.id,
    'res_ids': [(0,0, {
                    'partner_id': obj.partner_id.id,   #give id of partner 
                    'first_id': obj.first_id.id   #give id of first
             })]
    }
    self.pool.get('first.class').create(cr, uid, vals)
Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • Well the problem is I don't have id of `first`, because I will be creating `first` object, passing values from `second`. I should give `id` of `second` to first, because if I would give `first_id` its id, so I'm like assigning `first` id to `first` object, but as I said I need to assign `second` objects id to first. – Andrius Apr 02 '14 at 06:38
  • Your `first.class` has a relationship with `second.class`. So first need to `create` record for `Second class`. You must have record for `second class`. And than assign to the `First class`. Wait i updated my answer. – Bhavesh Odedra Apr 02 '14 at 06:45
  • When I create `first.class` object, I already have `second.class` object. First object is being created when one condition is met in second object. So I already have second object, I don't need to create it. And as I said I'm creating from second class model. What I don't have is first class object. **Update**. So that `obj.id` is id of second class object (which I already have, but don't know how to pass in `first.class` object when creating it) – Andrius Apr 02 '14 at 06:59
  • have your tried above method ? What output comes ? Put that method into `second.class` object. – Bhavesh Odedra Apr 02 '14 at 07:04
  • Hm, it seems to be working. I just don't understand where does `second.class` get `first_id` to pass, when it does not exist yet? Also you should fix typo errors if someone would need that answer too. after vals, `=` is needed and, after first key value pari, `,` is needed. – Andrius Apr 02 '14 at 07:19
  • ohh, because i directly create method here, didn't test. That'y you get type error `,`. I know the code is perfect that'y i said tried it. Thanks. – Bhavesh Odedra Apr 02 '14 at 07:26
  • After testing it more, I found it was not working like I expected. I didn't see that it created new record for `second.class` every time method was called before. As I said before, it should use current `second.class` object and pass it's id to `first.class` that is being created. But this method does not pass current `second.class` object id, it creates new object every time. – Andrius Apr 02 '14 at 08:20
  • yes, because we do calculation in some method. And `create` method generate `new record` to prevent this you may use `write` method which will be updated your record. `Don't use create method in some method.` Take id of `first.class` and write on that id above code. – Bhavesh Odedra Apr 02 '14 at 08:27
  • Yeah, just figured that :). I tried to create `first.class` object before and then tried to use its id to write in `second.class`, but I thought it returned object itelf, so I used something like that: `obj = self.pool.get('first.class').create(cr, uid, {})`. And then I tried to get it's id like `obj.id`, when actually `obj` was `id` itself. Now that I figured, it was easy to rewrite method, so I it would write that id into `second.class` and everything updates properly. Anyway thanks for help. – Andrius Apr 02 '14 at 08:37
4

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
walterbio
  • 51
  • 2
1

You can easily save record in this class

first_class_obj.create(cr,uid,{
    'partner_id':partner.id, 
     res_ids : [
                  {'partner_id':parner_1.id},
                  {'partner_id':parner_2.id},
                  {'partner_id':parner_3.id},.....
               ]
     })

here you can easily pass list of second class object

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
  • Why for `res_ids`, keys inside dictionary are `partner_id`? It should be something with `second.class`. I tried like this `'res_ids: [{'id': obj.id}]`, then it gave error `KeyError: 0`, if I written this `'res_ids': {'id': obj.id}`, it passes without errors, but does not write into one2many field. – Andrius Apr 02 '14 at 06:25