3

Can someone give me an example to manipulate a many2many field using the new API? I've tried to read the Documentation to no avail.

Here are my example classes:

from openerp import models, fields, api, _

class example_class_one(models.Model):

    _name           = "example.class.one"

    name           = fields.Char('Name')
    value          = fields.Float('Value')

example_class_one()

class example_class_two(models.Model):

    _name           = "example.class.two"

    name                = fields.Char('Name')
    example_class_ones  = fields.Many2many('example.class.one',string='Example Class Ones')

    @api.one
    def test(self):
        #CREATES SOME example_class_ones and assign them to self
        #MANIPULATE SOME example_class_ones and save them
        #DELETE SOME example_class_ones from self
        pass

example_class_two()
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
William Wino
  • 3,599
  • 7
  • 38
  • 61

2 Answers2

1

In Odoo 8 the new ORM API is much nicer that the previous one (with all these boring (cr, uid, ids, ..) parameters). One of the big advantages for me with this new API is the fact that we are now working with objects, not ids.

All you need with the new methods is the self parameter. You can iterate over it - it's among other things also a collection of odoo objects.

And there is also one magic variable - self.env which is of type Environment and contains all this cr, uid, etc. stuff. It contains also a collection of all known models - that's what you need.

So why don't you try this way:

@api.one
def test(self):
    model_one = self.env['example.class.one']
    model_one.create({'name': 'Some ONE object', 'value': 2.0})
    ones = model_one.browse([1, 3, 5])
    ones2 = model_one.search([('name', '=', 'Some name')])
    # You can imagine - search() return also objects! :)
    ones2[0].unlink()
    # Or, to deal with you many2many field...
    self.example_class_ones += model_one.new({
        'name': 'New comer to the many2many relation',
        'value': 666.0})

Hope that answers you question.

Andrei Boyanov
  • 2,309
  • 15
  • 18
  • can I do this: `self.example_class_ones.delete()` to delete all the example_class_ones in self? – William Wino Sep 09 '15 at 03:46
  • I got this error `AttributeError: 'example.class.one' object has no attribute 'delete'` – William Wino Sep 09 '15 at 04:10
  • Because it's not delete, it's unlink(). Sorry for this mistake. I'm correcting the answer now. – Andrei Boyanov Sep 09 '15 at 06:52
  • Thank you! You've been really helpful. If you don't mind, you can try to answer another question of mine, this one is harder I think. Just take a look at this http://stackoverflow.com/questions/32475648/odoo-method-synchronization-compute-method-collision – William Wino Sep 09 '15 at 09:20
0

You can refer to my case as below, either on @api

@api.onchange('opportunity_id')
    def _get_description(self):
        if self.opportunity_id.id:
            self.x_description = self.opportunity_id.x_description 

or declare as setup relationship and field (related) as link below

Pass custom field values from oppertunity to quotation in odoo 10

Khristos
  • 973
  • 2
  • 11
  • 22
chau ho
  • 11
  • 4