Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4516 Lượt xem

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả Câu trả lời hay nhất

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?

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 4 24
1592
1
thg 2 19
3267
1
thg 6 25
15481
3
thg 4 25
5685
0
thg 4 25
737