1

I have this method, which should create a new stock.location every time a new car model is added to fleet.vehicle class in Odoo v9 community.

class fleet_vehicle(models.Model):
_inherit = 'fleet.vehicle'

@api.onchange('model_id')
def crear_location(self):
    self.env['stock.location'].search([('name', '=', self.model_id)])
    self.env['stock.location'].create({'name': self.model_id})
    return crear_location

location_id = fields.Many2one("stock.location", string="Almacén Origen", store=True, compute=crear_location)

But, the field on fleet.vehicle form is readonly, for some reason, I mean, I can't select, nor create anything, it's just there.

What am I missing here?

EDIT

I partially solved the issue by putting the attribute readonly=False on the location_id field, ut still, the stock.location isn't created whatsoever.

NeoVe
  • 3,857
  • 8
  • 54
  • 134

1 Answers1

1

There are three things that may cause the problems:

1/ onchange in Odoo only return warning,domain or values. Refer to this post for more info

2/ location_id has 'compute' attribute, 'compute' often goes with @api.depends, not onchange. Please refer to this post for more explainations.

3/ In Odoo v9, self is recordset, you should use for loop to access single record

You should try your code like this (if using onchange, no need to use compute)

@api.onchange('model_id')
def crear_location(self):
    for record in self:
        location = self.env['stock.location'].search([('name', '=', record.model_id)])
        if not location:
            location = self.env['stock.location'].create({'name': record.model_id})
        record.location_id = location

location_id = fields.Many2one("stock.location", string="Almacén Origen")
Community
  • 1
  • 1
thangtn
  • 856
  • 6
  • 7
  • Hi, very cool, but how it is created? Should I add this method to a button or something?, location_id field doesn't show anthing new and it doesn't creates the stock.location with this method, I think you are right, but how to use it? , sorry just wanting to know, Thank You again – NeoVe Mar 06 '17 at 14:22
  • That's why I was using compute, I know it wasn't right, but the location should be created when I save the record somehow, I'm just asking, should I open a new question? – NeoVe Mar 06 '17 at 14:27