Skip to Content
Odoo Menu
  • Prisijungti
  • Išbandykite nemokamai
  • Programėlės
    Finansai
    • Apskaita
    • Pateikimas apmokėjimui
    • Sąnaudos
    • Skaičiuoklė (BI)
    • Dokumentai
    • Pasirašymas
    Pardavimai
    • CRM
    • Pardavimai
    • Kasų sistema - Parduotuvė
    • Kasų sistema - Restoranas
    • Prenumeratos
    • Nuoma
    Svetainės
    • Svetainių kūrėjimo įrankis
    • El. Prekyba
    • Internetinis Tinklaraštis
    • Forumas
    • Tiesioginis pokalbis
    • eMokymasis
    Tiekimo grandinė
    • Atsarga
    • Gamyba
    • PLM
    • Įsigijimai
    • Priežiūra
    • Kokybė
    Žmogaus ištekliai
    • Darbuotojai
    • Įdarbinimas
    • Atostogos
    • Įvertinimai
    • Rekomendacijos
    • Transporto priemonės
    Rinkodara
    • Socialinė rinkodara
    • Rinkodara el. paštu
    • SMS rinkodara
    • Renginiai
    • Rinkodaros automatizavimas
    • Apklausos
    Paslaugos
    • Projektas
    • Darbo laiko žiniaraščiai
    • Priežiūros tarnyba
    • Pagalbos tarnyba
    • Planavimas
    • Rezervacijos
    Produktyvumas
    • Diskucija
    • Patvirtinimai
    • IoT
    • VoIP
    • Žinių biblioteka
    • WhatsApp
    Trečiųjų šalių programos Odoo Studija Odoo debesijos platforma
  • Pramonės šakos
    Mažmeninė prekyba
    • Knygynas
    • Drabužių parduotuvė
    • Baldų parduotuvė
    • Maisto prekių parduotuvė
    • Techninės įrangos parduotuvė
    • Žaislų parduotuvė
    Food & Hospitality
    • Barai ir pub'ai
    • Restoranas
    • Greitasis maistas
    • Guest House
    • Gėrimų platintojas
    • Hotel
    Nekilnojamasis turtas
    • Real Estate Agency
    • Architektūros įmonė
    • Konstrukcija
    • Estate Managament
    • Sodininkauti
    • Turto savininkų asociacija
    Konsultavimas
    • Accounting Firm
    • Odoo Partneris
    • Marketing Agency
    • Teisinė firma
    • Talentų paieška
    • Auditai & sertifikavimas
    Gamyba
    • Textile
    • Metal
    • Furnitures
    • Maistas
    • Brewery
    • Įmonių dovanos
    Sveikata & Fitnesas
    • Sporto klubas
    • Akinių parduotuvė
    • Fitneso Centras
    • Sveikatos praktikai
    • Vaistinė
    • Kirpėjas
    Trades
    • Handyman
    • IT įranga ir palaikymas
    • Saulės energijos sistemos
    • Shoe Maker
    • Cleaning Services
    • HVAC Services
    Kiti
    • Nonprofit Organization
    • Aplinkos agentūra
    • Reklaminių stendų nuoma
    • Fotografavimas
    • Dviračių nuoma
    • Programinės įrangos perpardavėjas
    Browse all Industries
  • Bendrija
    Mokykitės
    • Mokomosios medžiagos
    • Dokumentacija
    • Sertifikatai
    • Mokymai
    • Internetinis Tinklaraštis
    • Tinklalaidės
    Skatinkite švietinimą
    • Švietimo programa
    • Scale Up! Verslo žaidimas
    • Aplankykite Odoo
    Gaukite programinę įrangą
    • Atsisiųsti
    • Palyginkite versijas
    • Leidimai
    Bendradarbiauti
    • Github
    • Forumas
    • Renginiai
    • Vertimai
    • Tapkite partneriu
    • Services for Partners
    • Registruokite jūsų apskaitos įmonę
    Gaukite paslaugas
    • Susiraskite partnerį
    • Susirask buhalterį
    • Susitikti su konsultantu
    • Diegimo paslaugos
    • Klientų rekomendavimas
    • Palaikymas
    • Atnaujinimai
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Gaukite demo
  • Kainodara
  • Pagalba

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Apskaita
  • Atsarga
  • PoS
  • Projektas
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Žymos (View all)
odoo accounting v14 pos v15
About this forum
Pagalba

Prevent navigation if records are not save

Prenumeruoti

Get notified when there's activity on this post

This question has been flagged
v15
2 Replies
3128 Rodiniai
Portretas
Antonio Krsnik

I am trying to implement warning to user when there is a change in the records, but Save button is not hit and they are navigation to some other screen or closing the window. 

I tried multiple things


Adding flag like below

has_unsaved_changes = fields.Boolean(string="Has Unsaved Changes", default=False)


which should be modified onchange of certain fields to give validation error:


    @api.constrains('has_unsaved_changes')

    def _check_unsaved_changes(self):

        for record in self:

            if record.has_unsaved_changes:

                _logger.warning(f"ValidationError: Unsaved changes detected for record {record.id}.")

                raise ValidationError("You have unsaved changes. Please save your changes before proceeding.")

Which then should be modified to true when button is pressed. But no write or create function work on it, as it immediately triggers the warning 

Annoying thing is also that Odoo is automatically trying to save when you navigate. 

Another thing I tried is using custom button and context

But even when I introduce a button it still get an error when user clicks it:


def write(self, vals):

        """Override write to prevent automatic saving unless explicitly triggered."""

        if not self.env.context.get('is_user_action', False):

            raise UserError("Automatic save is not allowed. Please save explicitly using the appropriate action.")

        if 'has_unsaved_changes' in vals or any(field in vals for field in self._fields):

            vals['has_unsaved_changes'] = False

        return super(CreateTargetMetrics, self).write(vals)


    def save_changes(self):

        """Button action to save changes."""

        for record in self:

            # Save new record

            _logger.info("********Creating new record ********")

            _logger.info("record._convert_to_write(record._cache)==", record._convert_to_write(record._cache))

            record.with_context(is_user_action=True).create(record._convert_to_write(record._cache))


I added the logs also, and when pressing custom button I don't get any, meaning the button is not even triggered

0
Portretas
Atmesti
Christoph Farnleitner

You could check out how this is done for the Settings page - any change there will lead to a warning, when navigating elsewhere - and adapt this feature to your needs.

Portretas
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,

Please refer to the module:

1. https://apps.odoo.com/apps/modules/18.0/auto_save_restrict

Displays a popup asking if you want to save changes when attempting to navigate away from a record with unsaved changes.


Hope it helps




0
Portretas
Atmesti
Portretas
D Enterprise
Best Answer
  1. Use Odoo's JS window.onbeforeunload event to detect unsaved changes on the client and warn the user.
  2. Hook into Odoo's FormView JS to detect changes to form fields and set a flag in the browser.
  3. Allow save normally (don’t block server-side writes) — just warn client-side.
How to implement this in Odoo 15/16/17+ (example concept):

1. Create a small JS module extending FormView
odoo.define('your_module.form_warning', function (require) {

    "use strict";


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


    FormController.include({

        init: function () {

            this._super.apply(this, arguments);

            this.hasUnsavedChanges = false;

        },


        _onFieldChanged: function (event) {

            this.hasUnsavedChanges = true;

            this._super.apply(this, arguments);

        },


        _onSave: function () {

            this.hasUnsavedChanges = false;

            return this._super.apply(this, arguments);

        },

    });


    window.addEventListener('beforeunload', function (e) {

        // If user has unsaved changes, show warning dialog

        if (odoo.__DEBUG__.services.form_controller && odoo.__DEBUG__.services.form_controller.hasUnsavedChanges) {

            var confirmationMessage = 'You have unsaved changes. Are you sure you want to leave?';

            e.returnValue = confirmationMessage; // Gecko + IE

            return confirmationMessage; // Webkit, Chrome

        }

    });

});

This example overrides the form controller to track changes, reset on save, and warn on page unload.
Include this JS file in your module manifest:
'assets': {

    'web.assets_backend': [

        'your_module/static/src/js/form_warning.js',

    ],

},

On the server side:

  • Remove the constrains and write overrides related to unsaved changes flag.
  • Keep the database model simple; no need for has_unsaved_changes boolean.
  • Let Odoo handle saves normally.

0
Portretas
Atmesti
Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registracija
Related Posts Replies Rodiniai Veikla
Creating user creates duplicated partner_id
v15
Portretas
Portretas
1
rugs. 25
3044
Sales order line comment to Purchase order line comment Solved
v15
Portretas
Portretas
3
liep. 25
4353
attribute is not applied when dynamically rendering
v15
Portretas
1
geg. 25
2459
Odoo Custome Module Language Creation
v15
Portretas
Portretas
Portretas
Portretas
4
geg. 25
3872
Odoo 15 previous question papers/practice tests. Solved
v15
Portretas
Portretas
1
vas. 25
8103
Bendrija
  • Mokomosios medžiagos
  • Dokumentacija
  • Forumas
Atvirasis kodas
  • Atsisiųsti
  • Github
  • Runbot
  • Vertimai
Paslaugos
  • Odoo.sh talpinimas
  • Palaikymas
  • Atnaujinti
  • Pritaikytas programavimo kūrimas
  • Švietimas
  • Susirask buhalterį
  • Susiraskite partnerį
  • Tapkite partneriu
Apie mus
  • Mūsų įmonė
  • Prekės ženklo turtas
  • Susisiekite su mumis
  • Darbo pasiūlymai
  • Renginiai
  • Tinklalaidės
  • Internetinis Tinklaraštis
  • Klientai
  • Teisinis • Privatumas
  • Saugumas
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo yra atvirojo kodo verslo programų rinkinys, kuris apima visas įmonės poreikius: CRM, El. Prekybą, Apskaitą, Atsargų, Kasų sistemą, Projektų valdymą ir kt.

Unikali Odoo vertės pasiūla – būti tuo pačiu metu labai lengvai naudojama ir visiškai integruota sistema.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now