Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
3 Antworten
1910 Ansichten

Hi Kids

Im trying to make a computed field and im looking for some assistance 


I have 3 prices in a model

Price_1

Price_2

Price_3

a field called selector with 3 values: No repair, Processing, Device repair

and my final float field called Final Price.

Im looking to get in the Final_Price field the price based on the selection.

If no repair then the computed field will get the Value of Price_1

If Processing then the computed field will get the Value of Price_2

If Device repair then the computed field will get the Value of Price_3


Thank in advance

Avatar
Verwerfen
Autor

this is what i have so far but i get error at elif line

for record in self:
if str(record.x_studio_repair_category) == '1':
record['x_studio_final_repair_price'] = record.x_studio_no_repair
elif str(record.x_studio_repair_category) == '2':
record['x_studio_final_repair_price'] = record.x_studio_processing
else str(record.x_studio_repair_category) == '3':
record['x_studio_final_repair_price'] = record.x_studio_device_repair

Beste Antwort

Hi,

Try like below

Price_1 = fields.Float(string="Price 1")
Price_2 = fields.Float(string="Price 2")
Price_3 = fields.Float(string="Price 3")
selector = fields.Selection(
[('no_repair', 'No Repair'), ('processing', 'Processing'), ('device_repair', 'Device Repair')],
string="Selector"
)
final_price = fields.Float(string="Final Price", compute='_compute_final_price')

@api.depends('selector')
def _compute_final_price(self):
for record in self:
if record.selector == 'no_repair':
record.final_price = record.Price_1
elif record.selector == 'processing':
record.final_price = record.Price_2
elif record.selector == 'device_repair':
record.final_price = record.Price_3
else:
record.final_price = 0.0

Hope it helps

Avatar
Verwerfen
Autor Beste Antwort


Here is a screenshot


Avatar
Verwerfen

Hi Pete: The indentation of the "elif"s need to be fixed. They need to be aligned with the "if".

Beste Antwort

Hi,

In your model try like this:

Price_1 = fields.Float(string='Price 1')
Price_2 = fields.Float(string='Price 2')
Price_3 = fields.Float(string='Price 3')
selector = fields.Selection(
[('no_repair', 'No repair'), ('processing', 'Processing'), ('device_repair', 'Device repair')],
string='Selector'
)
final_price = fields.Float(string='Final Price', compute='_compute_final_price')

@api.depends('selector')
def _compute_final_price(self):
for record in self:
if record.selector == 'no_repair':
record.final_price = record.Price_1
elif record.selector == 'processing':
record.final_price = record.Price_2
elif record.selector == 'device_repair':
record.final_price = record.Price_3


Hope this will help you

Thanks

Avatar
Verwerfen
Autor

Here is what i did:
created the fields.
x_studio_no_repair (yours is price_1)
x_studio_processing (yours is price_2)
x_studio_device_repair (yours is price_3)

Created a float field for my final price and named it x_studio_final_repair_price
set my boolean field ('no_repair', 'No repair'), ('processing', 'Processing'), ('device_repair', 'Device repair') with the name of x_studio_repair_category

Set my dependencies to x_studio_no_repair,x_studio_processing,x_studio_device_repair,x_studio_repair_category

and here is my code in the final repair price field in odoo:

for record in self:
if record.x_studio_repair_category == 'no_repair':
record['x_studio_final_repair_price'] = record.x_studio_no_repair
elif record.x_studio_repair_category == 'processing':
record['x_studio_final_repair_price'] = record.x_studio_processing
elif record.x_studio_repair_category == 'device_repair':
record['x_studio_final_repair_price'] = record.x_studio_device_repair

at this point when i try to open the model i have the field in i get this error:

elif record.x_studio_repair_category == 'processing':
^
SyntaxError: invalid syntax

looks like the elif syntax is wrong