Search code examples
odoo

Odoo write to field not part of the current view


I want to update the field done_date which is part of the project.task form view when pulling a Kanban tile on the stage that is indicated as being the final stage.

My code below works fine if the field is part of the Kanban view but fails to write if the field is only part of the task form view and not part of the Kanban project view.

The done_date field should be written even without being part of the Kanban view.

models.py

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class project_set_end_date(models.Model):
    inherit = 'project.task.type'

    last_stage = fields.Boolean(string="Final stage")

class project_set_end_date(models.Model):
    _inherit = 'project.task'

   @api.onchange('stage_id')
   def _set_end_date(self):
       if self.stage_id.last_stage:
           self.date_finished = fields.Datetime.now()

views.py

<odoo>
<data>
<!-- explicit list view definition -->
<record model="ir.ui.view" id="project_set_end_date">
  <field name="name">project.task.type.form</field>
    <field name="model">project.task.type</field>
    <field name="inherit_id" ref="project.task_type_edit"/>
    <field name="arch" type="xml">
      <xpath expr="//field[@name='fold']" position='after'>
      <field name="last_stage"/>
      </xpath>
  </field>
</record>
</data>
</odoo>

Solution

  • Odoo doesn't write to a field that doesn't exist in the current view. So I suggest adding the field but with the attribute invisible = True to avoid showing it:

    <field name="your_field" invisible="True"/>