Search code examples
odoo-8odooodoo-10odoo-9odoo-11

context doesn't update value of type


I have these 2 buttons in form view:

<button name="%(action_view_task_make_situation)d" string="Create work situation" type="action" states="open" context="{'type': 'situation'}"/>
<button name="%(action_make_general_final_count)d" string="Create Final General Count" type="action" states="done" context="{'type': 'final_count'}"/> 

with these actions:

<record id="action_view_task_make_situation" model="ir.actions.act_window">
    <field name="name">Make Situation</field>
    <field name="res_model">task.make.situation</field>
    <field name="type">ir.actions.act_window</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="target">new</field>
    <field name="context">{'type':True}</field>
</record>
<record id="action_make_general_final_count" model="ir.actions.act_window">
    <field name="name">Make General Final Count</field>
    <field name="res_model">task.make.situation</field>
    <field name="type">ir.actions.act_window</field>
    <field name="view_type">form</field>
    <field name="view_mode">form</field>
    <field name="target">new</field>
    <field name="context">{'type':False}</field>
</record>   

now I have task.make.situation model:

class TaskMakeSituation(models.TransientModel):

    _name = "task.make.situation"

    type = fields.Char(compute = "compute_type", string="Type", readonly= True)

    @api.multi
    def compute_type(self):
        if self._context.get('type', True):
            return "situation"
        else:
            return "final_count"

But when I click one of the buttons the wizard appears with an empty type field.


Solution

  • Compute method have to "write" their values directly into the records:

    @api.multi
    def compute_type(self):
        context_type = self._context.get('type', True)
        for record in self:
            if context_type:
                record.type = "situation"
            else:
                record.type = "final_count"
    

    Aside from this, your solution should simply use default values for field type. First change your field to a normal char field:

    type = fields.Char(string="Type", readonly=True)
    

    Little hint: the parameter string isn't necessary in this example, because newer Odoo versions will use the field name to generate the labels (string becomes the field label) for example: name will get Name or partner_id will get Partner (_id will be dropped).

    Now change the button context, use default_type:

    <button name="%(action_view_task_make_situation)d" 
        string="Create work situation" type="action" states="open" 
        context="{'default_type': 'situation'}"/>
    <button name="%(action_make_general_final_count)d"
        string="Create Final General Count" type="action" states="done"
        context="{'default_type': 'final_count'}"/>
    

    The prefix default_ in combination with a field name, will be used in later default extraction for the creation of a record.