Reputation: 3897
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.
Upvotes: 1
Views: 108
Reputation: 876
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")
Upvotes: 1