Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
1299 Vistas

I want to combine two fields

field 1 = 100

field 2 = 200

I want the result field to read 100.200


What is the python code to be placed in the compute field of studio?

Are the fields to be char or integer?



Avatar
Descartar
Autor Mejor respuesta

Odoo Studio

This is the compute line

in compute_method(self):
    if self.x_studio_prefix_division and self.x_studio_suffix_division:
    self.result = '.'.join([self.x_studio_prefix_division, self.x_studio_suffix_division])


This is the error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 644, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 302, in _handle_exception
    raise exception.with_traceback(None) from new_cause
  File "", line 1
    in compute_method(self):
    ^
SyntaxError: invalid syntax


Avatar
Descartar
Mejor respuesta

for the question "Are the fields to be char or integer?" it doesn't matter if you keep field 1 and 2 as integer or char as you can always typecast at the time of computation, The logic to make the result as suggested depends on either you want the result as string "100.200" or in float 100.200, here is the logic for both:

1) if result you want should be in string:
=> in compute_method(self):
         if self.field1 and self.field2:
            self.result = self.field1 + '.' + self.field2
                                       OR
            self.result = '.'.join([self.field1, self.field2])
2) if result you want should be in float:
=> Simply typecast the string to float using float() method:
            self.result = float('.'.join([self.field1, self.field2]))
        [NOTE: this will give you excluding trailing zeroes as for float after decimal trailing zeroes doesn't count but you           can format the displaying numbers using digits attribute in view or field or using you custom unit of measure]

If you think my answer reaches to solution of you problem, please validate it and upvote, Thanks.  

Avatar
Descartar