-1

I need to save a record and open a form in edit mode after on_change event.

See the picture before on_change event Before save

See the picture after on_change event. After save

I need to save record after on_change event. And i need that the form will be redraw in edit mode.

The class:

from openerp import models, fields, api

class Master(models.Model):
    _name = 'att.master'

    name = fields.Char("Name")
    qty = fields.Integer("Qty")

    @api.onchange('name')
    def _on_change_name(self):
        if self.name:
            self.qty += 100
            print 'The on_change event was happened'
            self.env['att.master'].create({'name': self.name, 'qty': self.qty} )

The view:

<?xml version="1.0" encoding="UTF-8"?>
 <openerp>
    <data>
        <!-- form AGREEMENT -->
        <record model="ir.ui.view" id="att_master_view_form">
            <field name="name">Master</field>
            <field name="model">att.master</field>
            <field name="arch" type="xml">
            <form string="Master">
                    <sheet>
                        <group colspan="4">
                                <field name="name"/>
                                <field name="qty"/>
                                <field name="id"/>
                        </group>
                    </sheet>
            </form>
            </field>
        </record>

        <record model="ir.ui.view" id="att_master_view_tree">
            <field name="name">List of master</field>
            <field name="model">att.master</field>
            <field name="arch" type="xml">
                <tree string="List of master">
                    <field name="name"/>
                    <field name="qty"/>
                    <field name="id"/>
                </tree>
            </field>
        </record>

        <!-- window action -->
        <record model="ir.actions.act_window" id="att_master_list_action">
            <field name="name">Master</field>
            <field name="res_model">att.master</field>
            <field name="view_mode">tree,form</field>
        </record>

        <menuitem id="att_master_menuitem" name="Master"
                  parent="att_menu_agreement"
                  action="att_master_list_action"/>
    </data>
</openerp>

How can i do this?

atimtim
  • 1
  • 5

1 Answers1

0

To write the new qty you have just to use:

self.qty = new_value

From onchange documentation:

onchange methods work on virtual records assignment on these records is not written to the database, just used to know which value to send back to the client

The new quantity will be saved to the database when you click on Save button

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • I think this code doesn't make a real ID. I need a real ID instead a NewID object in self.id field – atimtim Nov 05 '15 at 13:40