Skip to Content
Menu
This question has been flagged
2 Replies
3229 Views

I want to create Model (car) name like, Telsa / Model X, and I found this piece of code of Fleet module doing that.

FleetVehicleModel class (models.Model) 
_name = 'fleet.vehicle.model'
_description = 'Model of a vehicle'
_order = 'desc'

name = fields.Char ('Model name', required = True)
brand_id = fields. Many2one ('fleet.vehicle.model.brand', 'Make', required = True, help = 'Make of the vehicle')
vendors fields.Many2many = ('res.partner', 'fleet_vehicle_model_vendors',' model_id '' partner_id ', string =' Vendors')
image = fields.Binary (related = 'brand_id.image', string = "Logo", readonly = False)
image_medium = fields.Binary (related = 'brand_id.image_medium', string = "Logo (medium) ",
image_small = fields.Binary (related = 'brand_id.image_small', string = "Logo (small)", readonly = False)

@ api.multi
@ api.depends ('name', 'brand_id')
def name_get (self):
res = []
for record in self:
name = record.name
if record.brand_id.name:
name = record.brand_id.name + '/' + name
res.append ((record.id, name))
return res

I do not understand how this code works when I choose the model then press Save button, it will create a new name under the form "Brand / Model" and replace the old name I filled.
And I did not see any 'computed field' in this model.
I figure out by 'print (res)', but I do not understand how this can link 'record.id' in 'res []' to find the new name.
I also can not find any new fields in Database too.


I will appreciate if someone can explain it.
Many thanks

Avatar
Discard
Best Answer

Hi,

What is your doubt about ? Is it about the name_get function defined ? The methods is called by Odoo core itself, probably you  will never end up calling those by yourself or not needed to have a compute field for that. Those methods are called by some widgets like many2one to get the display name of the record.


Thanks

Avatar
Discard
Author

In short, I just want to know Where is list res[] in get_name func stored?

Author Best Answer

Well, I want to create a name of a car in the form like "Model/Brand", e.g: ModelX/Telsa. I call this 'formed name'.

Generally, I will create a field: 

Car_name = fields.Char(computed='get_name')

brand_id = fields.Many2one('car.brand')

model_id = fields.Many2one('car.model')

then I will define a 'computed function' to create 'car_name' like I want.

@api.depends('brand_id', 'model_id')

def get_name(self):

car_name = str(brand_id.name) + '/' + str(model_id.name)

then, I can show car_name in my tree.view or form.view. 

It is ok if I do like that.

BUT, when I read the code in Fleet module, there isn't a 'computed field' (to form the name like I want).

If you read name_get func in Fleet code (like, I quoted above), they just use a list res[], then stored the formed name and the id of the record. Then the 'formed name' appears in the tree.view or form.view after I press the Save button.

I just print the list 'res[]', and it stored the id and the 'formed name' as: [(1, ModelX/Telsa),(2, Vios/Toyota)....]

I just dont understand how they can call the formed name through 'res' list only by the id of the record. I checked in the SQL but cant find any field store 'formed name'.

In short, I just want to know Where is list res[] in get_name func stored?

Avatar
Discard
Related Posts Replies Views Activity
0
Apr 24
330
1
Feb 19
1977
2
Jul 24
413
1
Jul 24
343
1
Jan 24
422