在为该记录打开表单视图时如何更新记录?

时间:2015-10-16 04:05:25

标签: odoo odoo-8

我想通知用户记录已更新,因此我创建了一个指示更改的布尔字段。我还对树进行了颜色编码,这样当布尔字段设置为true时,颜色将改变以通知用户。

现在,当用户单击树视图中的记录并打开表单视图时,我必须更新布尔字段。我该怎么做?

这是我创建的虚拟模型和虚拟视图:

型号:

    from openerp import models, fields, api, _

    class form_record_update_test_subject_a(models.Model):

        _name           = "form.record.update.test.subject.a"

        name            = fields.Char('Name')
        is_blue         = fields.Boolean('Is Blue')

    form_record_update_test_subject_a()

查看:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <menuitem name="Testing Module"
            id="testing_module_menu"
            sequence="1"/>

        <menuitem id="form_record_update_menu" name="Form Record Update Test" parent="testing_module_menu" sequence="1"/>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_form_view">
            <field name="name">form.record.update.test.subject.a.form.view</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">form</field>
            <field name="arch" type="xml">
                <form string="Form Request Update Test" version="8.0">
                    <sheet>
                        <group>
                            <field string="Name" name="name" class="oe_inline"/>
                            <field string="Is Blue" name="is_blue" class="oe_inline"/>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_tree_view">
            <field name="name">form.record.update.test.subject.a.tree.view</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">tree</field>
            <field name="arch" type="xml">
                <tree string="Class" colors="blue:is_blue==True">
                    <field string="Name" name="name"/>
                    <field string="Is Blue" name="is_blue"/>
                </tree>
            </field>
        </record>

        <record model="ir.ui.view" id="form_record_update_test_subject_a_search">
            <field name="name">form.record.update.test.subject.a.search</field>
            <field name="model">form.record.update.test.subject.a</field>
            <field name="type">search</field>
            <field name="arch" type="xml">
                <search string="Form Request Update Test Search">
                    <field string="Name" name="name"/>
                    <field string="Is Blue" name="is_blue"/>
                </search>
            </field>
        </record>

        <record id="form_record_update_test_subject_a_action" model="ir.actions.act_window">
            <field name="name">Form Request Update Test</field>
            <field name="res_model">form.record.update.test.subject.a</field>
            <field name="view_type">form</field>
            <field name="domain">[]</field>
            <field name="context">{}</field>
            <field name="view_id" eval="form_record_update_test_subject_a_tree_view"/>
            <field name="search_view_id" ref="form_record_update_test_subject_a_search"/>
            <field name="target">current</field>
            <field name="help">Form Request Update Test</field>
        </record>

        <menuitem action="form_record_update_test_subject_a_action" icon="STOCK_JUSTIFY_FILL" sequence="1"
            id="form_record_update_test_subject_a_action_menu"  parent="testing_module.form_record_update_menu"
        />
    </data>
</openerp>

1 个答案:

答案 0 :(得分:0)

当我在寻找类似的解决方案时偶然发现了这个问题,最后我自己弄清楚了,我想分享我的解决方案:

您将需要注册自己的JS资产。在模块的static / src / js文件夹中创建一个js文件。注册:

    <template id="assets_backend" name="MODULENAME assets" inherit_id="web.assets_backend">
        <xpath expr="." position="inside">
            <script type="text/javascript" src="/YOUR_MODULE_NAME/static/src/js/FILENAME.js" />
        </xpath>
    </template>

在javascript中,您需要编写以下内容:

odoo.define('dsw_dongle.sheet', function (require) {
"use strict";
var Model = require('web.Model');
// register here the model you would like to change
var my_model = new Model('YOUR_MODEL_NAME');
var FormView = require('web.FormView');
var framework = require('web.framework');

var myForm = FormView.include({
    load_record: function(record) {
        // This will be triggered every time the data inside the form will be loadd
        this._super.apply(this, arguments);
        // make sure that this is your view either by pulling it from 'this'
        // or from the 'record'. For demo I skipped this.
        var self = this;

        // If your update-procedure is taking longer, block the UI
        framework.blockUI();
        dongle_model.call('MODEL_FUNCTION_NAME', [record.id]).then(function(resp){
            // make sure that you return a nice value otherwise this will be 
            // an endless recursion
            if (resp === true) {
                self.reload();
            } else {
                // Remove the waiting screen
                framework.unblockUI();
            }
        });
     }
  });
});

希望这会帮助其他人在Odoo 10.0中寻找“ on_load”事件