Skip to Content
Menu
This question has been flagged

Hello, I am working with Odoo 16, and I was wondering if there is a way to update the entire web page, or to reload just the form view, when a field in the form changes (in my specific case, a Many2one field called "bank").

I'm asking this because I have a "documents" One2many field, in the same form where "bank" is, that automatically updates when "bank" changes.
In the Python method, decorated with @api.onchange('bank'), there are instructions that create documents in the database based on the selected bank.
The issue is that if I change "bank", after manually adding a document, the new documents created by the function are not displayed until I save the form or reload the page.


I tried using JavaScript, writing the following snippet:

odoo.define('my_crm.reload_on_bank_change', function (require) {
"use strict";

var FormController = require('web.FormController');

FormController.include({
_onFieldChanged: function (record, changes) {
if ('bank' in changes) {
// Reload the page if the "bank" field has changed
location.reload(true);
}
},
});
});

And also Python, using @api.onchange('bank') on my method and making it return the following:

return {
    'type': 'ir.actions.client',
    'tag': 'reload',
}


I have searched everywhere online and in the documentation, but I haven't been able to find a solution, and unfortunately, the above methods didn't work.
Thanks to anyone who will answer.

Avatar
Discard
Best Answer
 bus_service.subscribe is not function. How to fix?
Avatar
Discard
Best Answer

try this
at the end of python method:

self.env['bus.bus']._sendone(self.create_uid.partner_id, 'page_refresh', {
'type': response_type,
'title': "Reload",
'message': _(response_message),
'sticky': True,
'model_name': self._name,
'rec_id': self.id
})

in js:

=/* @odoo-module */

import { registry } from "@web/core/registry";
import { browser } from "@web/core/browser/browser";

export const simplePageRefresh = {
dependencies: ["bus_service", "notification"],
start(env, { bus_service, notification: notificationService }) {
bus_service.subscribe("page_refresh", ({ message, sticky, title, type, model_name, rec_id }) => {

if (`${browser.location}`.includes(`id=${rec_id}`) && `${browser.location}`.includes(`model=${model_name}`)) {
browser.location.reload();
} else {
console.log('cant locate correct page');
}

notificationService.add(message, { sticky, title, type });
});
bus_service.start();
},
};

registry.category("services").add("refresh_page", simplePageRefresh);


Avatar
Discard
Related Posts Replies Views Activity
1
Apr 24
2042
3
Jun 23
2457
1
May 24
1476
1
Sep 23
2210
1
Jun 23
3512