Skip to Content
Odoo Menu
  • Prijavi
  • Try it free
  • Aplikacije
    Finance
    • Knjigovodstvo
    • Obračun
    • Stroški
    • Spreadsheet (BI)
    • Dokumenti
    • Podpisovanje
    Prodaja
    • CRM
    • Prodaja
    • POS Shop
    • POS Restaurant
    • Naročnine
    • Najem
    Spletne strani
    • Website Builder
    • Spletna trgovina
    • Blog
    • Forum
    • Pogovor v živo
    • eUčenje
    Dobavna veriga
    • Zaloga
    • Proizvodnja
    • PLM
    • Nabava
    • Vzdrževanje
    • Kakovost
    Kadri
    • Kadri
    • Kadrovanje
    • Odsotnost
    • Ocenjevanja
    • Priporočila
    • Vozni park
    Marketing
    • Družbeno Trženje
    • Email Marketing
    • SMS Marketing
    • Dogodki
    • Avtomatizacija trženja
    • Ankete
    Storitve
    • Projekt
    • Časovnice
    • Storitve na terenu
    • Služba za pomoč
    • Načrtovanje
    • Termini
    Produktivnost
    • Razprave
    • Odobritve
    • IoT
    • Voip
    • Znanje
    • WhatsApp
    Third party apps Odoo Studio Odoo Cloud Platform
  • Industrije
    Trgovina na drobno
    • Book Store
    • Trgovina z oblačili
    • Trgovina s pohištvom
    • Grocery Store
    • Trgovina s strojno opremo računalnikov
    • Trgovina z igračami
    Food & Hospitality
    • Bar and Pub
    • Restavracija
    • Hitra hrana
    • Guest House
    • Beverage Distributor
    • Hotel
    Nepremičnine
    • Real Estate Agency
    • Arhitekturno podjetje
    • Gradbeništvo
    • Estate Management
    • Vrtnarjenje
    • Združenje lastnikov nepremičnin
    Svetovanje
    • Računovodsko podjetje
    • Odoo Partner
    • Marketinška agencija
    • Law firm
    • Pridobivanje talentov
    • Audit & Certification
    Proizvodnja
    • Tekstil
    • Metal
    • Pohištvo
    • Hrana
    • Brewery
    • Poslovna darila
    Health & Fitness
    • Športni klub
    • Trgovina z očali
    • Fitnes center
    • Wellness Practitioners
    • Lekarna
    • Frizerski salon
    Trades
    • Handyman
    • IT Hardware & Support
    • Sistemi sončne energije
    • Izdelovalec čevljev
    • Čistilne storitve
    • HVAC Services
    Ostali
    • Neprofitna organizacija
    • Agencija za okolje
    • Najem oglasnih panojev
    • Fotografija
    • Najem koles
    • Prodajalec programske opreme
    Browse all Industries
  • Skupnost
    Learn
    • Tutorials
    • Dokumentacija
    • Certifikati
    • Šolanje
    • Blog
    • Podcast
    Empower Education
    • Education Program
    • Scale Up! Business Game
    • Visit Odoo
    Get the Software
    • Prenesi
    • Compare Editions
    • Releases
    Collaborate
    • Github
    • Forum
    • Dogodki
    • Prevodi
    • Become a Partner
    • Services for Partners
    • Register your Accounting Firm
    Get Services
    • Find a Partner
    • Find an Accountant
    • Meet an advisor
    • Implementation Services
    • Sklici kupca
    • Podpora
    • Upgrades
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Get a demo
  • Določanje cen
  • Pomoč

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

  • CRM
  • e-Commerce
  • Knjigovodstvo
  • Zaloga
  • PoS
  • Projekt
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Ključne besede (View all)
odoo accounting v14 pos v15
About this forum
Pomoč

Equivalent javascript onLoad() - it is possible when form is loaded?

Naroči se

Get notified when there's activity on this post

This question has been flagged
domaindynamiconload
6 Odgovori
17105 Prikazi
Avatar
Zbik

This question is related to my previous question: conditional, dynamic field domain.

Example:

<field name="xxx"/>
<field name="yyy_ids" widget="many2many" domain="....DOMAIN......">

I know how to change the domain for yyy_ids when signal change is generated 

@api.onchange('xxx')
def do_xxx(self):

.... here code to change DOMAIN...

return {'domain': {'yyy_ids': ....DOMAIN_VALUE....}}

but how generate onchange signal, at the start, when form will be loaded?

0
Avatar
Opusti
Zbik
Avtor

Thanks, I will check soon.

Avatar
Zbik
Avtor Best Answer

@Mariusz, PARTLY SOLVED :(. Yes, we now have a new system behavior. Method onchage is fired when a record is created, but if record already exist onchange is not fired, regardless of the default and the new contents of the field xxx.

1
Avatar
Opusti
Mathieu Laflamme

Any workaround on that? I have the same challenge and I don't know what to do yet :(

Zbik
Avtor

I have no new messages. See: https://github.com/odoo/odoo/issues/5199

Avatar
Royal Administrator
Best Answer

Here is a way to cause the As Of date to immediately be modified when edit mode is entered:

  - a new FieldAutoJS widget that runs javascript from the xml when it becomes editable
  - declare a new writable field with create and write hooks that clear any attempted writes
  - include the field invisibly on the form and use the autojs widget to set any value
  - in onchange for the new field, if the field is not set, do whatever actions desired to initialize the form, e.g. set date

Javascript:

// 13.0.6.13.2-t8 jimays
// code--1299-edc6-4d86f0db-acb0-f9ee6cefae13--13.0.6.13.2-t8--7.55.20--5.22--XYqqm7*iTnaBOodPqhvqh1
//---------------------------------------------------------------------------------------------

odoo.define('carlisleofficesuites.thermostats', function (require){
'use strict';
var form_widget = require('web.form_widgets');
var core = require('web.core');

// gratitude https://www.odoo.com/forum/help-1/question/equivalent-javascript-onload-it-is-possible-when-form-is-loaded-75863
// gratitude https://www.odoo.com/forum/aide-1/question/how-to-customize-the-onclick-javascript-action-of-an-edit-button-odoo-10-125253
// gratitude https://www.odoo.com/fr_FR/forum/aide-1/question/onchange-method-works-before-something-change-on-field-odoo11-133024
// gratitude https://stackoverflow.com/questions/7650071/is-there-a-way-to-create-a-function-from-a-string-with-javascript
// gratitude https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
// which precisely asks the question, and also
// gratitude https://stackoverflow.com/questions/31187456/which-method-is-called-when-user-click-on-edit-button
// gratitude https://stackoverflow.com/questions/11970035/change-field-value-and-trigger-onchange-event-on-button-press
// gratitude https://www.odoo.com/forum/aide-1/question/how-to-trigger-an-action-onload-of-a-form-view-23593
// gratitude https://www.odoo.com/forum/help-1/question/override-a-field-s-value-on-form-load-108986
// gratitude https://www.odoo.com/forum/aide-1/question/default-method-trigger-my-api-onchange-method-why-151042
// "Since a default value is being loaded..., there is a change in value which will trigger the onchange method.
// So whenever the value in that field changes, even when the default value is loaded, it will trigger the onchange method."
// "Try creating a new invisible field without any value. In your onchange check whether this field is empty or not.
// If it is empty, write some value to this field and exit. If it is not empty continue with your onchange.
// I mentioned this because this is easy to understand. There may be better ways to accomplish the same thing."
// gratitude https://www.odoo.com/forum/aide-1/question/onchange-method-works-before-something-change-on-field-odoo11-133024
// "... when form is load, onchange method is automatically called."
// gratitude http://elico-corp.com.sg/2015/10/01/how-to-tech-dynamically-modify-your-view/
// gratitude https://stackoverflow.com/questions/39424027/adding-a-button-in-tree-view-odoo-8
// gratitude addons/web/static/src/js/views/form_view.js
// gratitude addons/web/static/src/js/views/form_widgets.js
// gratitude FieldsBoolean and FieldsPriority and FieldBinaryFile
//
// Pair with:
//
// <field name="auto" widget="autojs" autojs="your_function();"/?
//
// To force an onchange when Edit is clicked, field can add invisible="1" to the xml
// Couple this code with create and write hooks that always clear the field from the write vals.
//
// <field name="auto" widget="autojs" autojs="return 'auto';" invisible="1"/>
//
// as_of = fields.Datetime('As Of', required=True)
// auto = fields.Char('Auto')
//
// @api.onchange('auto')
// def onchange_auto(self):
// if not self.auto:
// # the as_of is visibly adjusted when Edit is clicked
// self.as_of = fields.datetime.now()
//
// @api.model
// def update_write_vals(self, vals):
// vals['as_of'] = fields.Datetime.now()
// if 'auto' in vals:
// del vals['auto']
//
// @api.model
// @api.returns('self', lambda value: value.id)
// def create(self, vals):
// self.update_write_vals(vals)
// return super(ThisModel, self).create(vals)
//
// @api.multi
// def write(self, vals):
// self.update_write_vals(vals)
// super(ThisModel, self).write(vals)

var FieldAutoJS = form_widget.FieldChar.extend({
start: function(){
true && console.log('FieldAuto.start: this.field: ' + JSON.stringify(this.field));
true && console.log('FieldAuto.start: this.options.model_field: ' + JSON.stringify(this.options.model_field));
var self = this;
var change_effective_readonly = function(){
if(!self.get("effective_readonly")){
true && console.log('FieldAuto: Auto field has become editable.');
if('autojs' in self.node.attrs){
true && console.log('FieldAuto: Setting value with javascript: ' + self.node.attrs.autojs);
var autojs_function = new Function(self.node.attrs.autojs);
var newValue = autojs_function(self);
var oldValue = self.get_value();
true && console.log('FieldAuto: Triggering change:value, oldValue: ' + oldValue + ', newValue: ' + newValue);
self.trigger("change:value", self, { oldValue: oldValue, newValue: newValue });
self.render_value();
}
}
};
this.on("change:effective_readonly", this, change_effective_readonly);
change_effective_readonly.call(this);
this._super.apply(this, arguments);
},
});

core.form_widget_registry.add('autojs', FieldAutoJS);
});

Python

class CarlisleOfficeSuites_Thermostat(models.Model):
_name = 'carlisleofficesuites.thermostat'
_rec_name = 'zone'

zone = fields.Char('Zone', required=True)

# 13.0.6.13.2-t8 The auto field is always blank in the database and always auto-set by the javascript upon entering form view edit mode.
# This trick makes the form have a changed value as soon as it opens for edit, which visibly pre-adjusts the As Of, which is cute,
# and more importantly marks the form as changed so a save actually calls the write hook, and the latest draft settings become active.

as_of = fields.Datetime('As Of', required=True)
auto = fields.Char('Auto')

@api.onchange('auto')
def onchange_auto(self):
_logger.debug('onchange_auto, self.auto: ' + unicode(self.auto))
if not self.auto:
​ # the as_of is visibly adjusted when Edit is clicked
            self.as_of = fields.datetime.now()
    @api.model
def update_write_vals(self, vals):
vals['as_of'] = fields.Datetime.now()
if 'auto' in vals:
del vals['auto']
    @api.model
@api.returns('self', lambda value: value.id)
def create(self, vals):
self.update_write_vals(vals)
return super(CarlisleOfficeSuites_Thermostat, self).create(vals)

@api.multi
def write(self, vals):
self.update_write_vals(vals)
super(CarlisleOfficeSuites_Thermostat, self).write(vals)
return True
0
Avatar
Opusti
Avatar
Mariusz Mizgier
Best Answer

Did you try: 1. apply this patch - https://github.com/odoo/odoo/commit/2067a206ecba9a8dbf7f76a55926be964e580e7e 2. move your xxx before yyy_ids in form view 3. set _defaults for xxx to False Didn't check that, but that should work - if not, maybe change in logic (defaults = True) so that this field does change it's value.

0
Avatar
Opusti
Enjoying the discussion? Don't just read, join in!

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

Prijavi
Related Posts Odgovori Prikazi Aktivnost
use dynamic value in domain
domain dynamic
Avatar
Avatar
Avatar
2
maj 24
8624
dynamic domain in one2many Solved
domain one2many dynamic
Avatar
Avatar
Avatar
2
maj 24
10090
How to set many2one domain based on many2many field in odoo
domain many2one dynamic
Avatar
Avatar
1
jan. 22
12869
Dynamic Domain in Windows Action Solved
domain purchase_order dynamic
Avatar
Avatar
1
jul. 20
16858
Dynamically Restrict Choices in One2Many Relation Based on Already Selected Values
domain domain_filter dynamic attributes
Avatar
0
mar. 24
1872
Community
  • Tutorials
  • Dokumentacija
  • Forum
Open Source
  • Prenesi
  • Github
  • Runbot
  • Prevodi
Services
  • Odoo.sh Hosting
  • Podpora
  • Nadgradnja
  • Custom Developments
  • Izobraževanje
  • Find an Accountant
  • Find a Partner
  • Become a Partner
About us
  • Our company
  • Sredstva blagovne znamke
  • Kontakt
  • Zaposlitve
  • Dogodki
  • Podcast
  • Blog
  • Stranke
  • Pravno • Zasebnost
  • Varnost
الْعَرَبيّة 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 is a suite of open source business apps that cover all your company needs: CRM, eCommerce, accounting, inventory, point of sale, project management, etc.

Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.

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