This question has been flagged
3 Replies
4112 Views

Hi guys, not even professionals are able to solve this problem  ? 

Is it really impossible to do ?

How to create in Odoo a series of conditions and depending on value change the way of how will be built a string based on other fields?

in php it's simple task and it may look like this:

if ($itype ="type1"){

$prod_name = $field2 . $field4 . $field12 };

if ($itype = "type2"){

$prod_name = $field1 . $field3 . $field6};

and so forth .... simple right ?

so based on this example can ANYONE show me how to do this ?

...

field_0 = self.field_0 or ' '

field_1 = self.field_1 or ' '

field_2 = self.field_2 or ' '

 

-> if value of selection field = some value:

name = ustr(field_0) + ' ' + ustr(field_1) + ' ' + ustr(field_2)

-> if value of selection field = other value:

name = ustr(field_5) + ' ' + ustr(field_7) + ' ' + ustr(field_12)

...

Do I'm on  a good way ?

onchange ?

def onchange_state(self, cr, uid, ids, state, context=None):
 if x = state:
  state if x == "value":
   return {name = ustr(field_0) + ' ' + ustr(field_1) + ' ' + ustr(field_2)}
and so forth ... ?

Is it really impossible ?

Avatar
Discard
Best Answer

Why this is so hard?

simply do:

def onchange_state(self, cr, uid, ids, state, field_0, field_1, field_2,field_3, context=None):
    if state == 'computer':
       return {'value': {'name': '%s %s %s'%(ustr(field_0),ustr(field_1))}}
elif state == 'monitor'
    return {'value': {'name': '%s %s'%(ustr(field_0),ustr(field_3))}}
return {'value':{}}

And define the on_change passing all the fields that you need


<field name="arch" type="xml">
<field name="field_0"/>
<field name="field_1"/>
<field name="field_2"/>
<field name="field_3"/>
<field name="state" on_change="onchange_state(state,field_0,field_1,field_2,field_3)"/>
</field>


Avatar
Discard
Best Answer

First, I'm not exactly clear on what "name" you're referring to.  Is it the value of the name field within a specific record, or the label of the field of a certain record?

If it's the value of the name field, that's incredibly easy.  Just use a function field to compute the name or a secondary display name.  Examine the display_name field in res.partner from the base module as an example of this behavior.

If it's the label of the field (e.g.: "Invoice Address" on a sale.order record), then yes it's still possible, but will be vastly more effort than it's worth.  You should consider something simpler in the view definition:

<field name="state" invisible="1"/>
<label for="my_field" attrs="{'invisible': [('state','!=','done')]}"/>
<label for="my_field" string="Alternate Name" attrs="{'invisible': [('state','=','done')]}"/>
<div>
    <field name="my_field"/>
</div>
Avatar
Discard
Author

This problem is already solved, i created a function which creates "name" combining values of the other fields. So at the end "name = ustr(field_0) + ' ' + ustr(field_1) + ' ' + ustr(field_2)" is working excellent. I'm very happy with it and also previously I used a similar solution which wasn't good that's why I changed it. Still don't know how to create conditions. Now name is created always using same set of fields and all i need now is find the way how to do it. All i want is: "fields.selection()" is a list of types and depends on the value chosen, adequate string of fields is assigned. For example: if selection = 'computer': this fields will be combined together: name = ustr(field_0) + '' + ustr(field_1) if selection = 'monitor': this fieldswill be combined: name = ustr(field_0) + ' ' + ustr(field_3) but for some reason it won't work. And this is all I'm asking for.How should i do this ? The "if".