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
Thanks, I will check soon.