pythonodooodoo-9

Pass a value between screens in odoo 9


I have a model with a many2one field which allows me to create new records, so I want to pass one of the values selected in the Model A to the floating creation window of the Model B.

I was reading about using context, but I'm not sure if that can be used in this case.

Model A - Feature
Desc         >Char
Alternatives > O2M(Alternative,feature_id) 

Model B - Alternative
Desc         >Char
feature_id   >M2O(feature)

EDIT I: Adding relations between my models

I'm actually using 4 models, version, feature, alternative and tag, I want to create a version for each feature I have, a feature have alternatives and every alternative have tags

In the version I want to select the feature and then can add the tags related to the alternatives of the feature.

**version**
feature_id      m2o>feature
desc            char
tag_ids         o2m>tag,alternative_id

**tag** 
caption         char
alternative_id  m2o>alternative

**alternative**
desc            char
feature_id      m2o>feature
tag_ids         o2m>tag,alternative_id

**feature**
desc            char
alternative_ids o2m>alternative,feature_id

Can I use the context to create new tags on a version? or I have to use a related fields?


Solution

  • to pass a value to the form of the m2o field:

       <field name="m2o_field_name" context="{'default_field_name': field_name}"/>
    

    example:

      <field name="partner_id" context="{'default_email': company_email}" />
    

    here in my model a have m2o partner_id and a field named company_email and when i click create and edit what ever the value of company_email will be passed to email field in partner form.

    you can pass static value like 'default_name': 'charif' or you can pass value of a field named field_example like this 'default_name': field_example

    EDITS:

    version -->  feature    <--- alternative  <---  tags
    

    ok first you need to create m2o from tags to features:

        # don't forget store=True so you can create a o2m 
        feature_id = fields.Many2one(related='alternative_id.feature_id', store=True, readonly=true)
    

    now create a o2m from features to tags.

        tag_ids = fields.One2many('tag.model.name', 'feature_id', 'Tags')
    

    and here you can create a related field from version to feature tags.

        tag_ids = fields.One2many(related='feature_id.tag_ids', readonly=true)