Skip to Content
Menu
This question has been flagged
1 Reply
645 Views

Hello!
I'm using odoo 17 community version.

In my model i have a Datetime field

(.py file)
r_date = fields.Datetime(string="R date", required=False)

(xml)
<field name="r_date "/>

Field is empty by default, it's okay.  In the previous version(ver.10), when you clicked on a field, it was filled with the current date and time value, but now a widget appears and the value is still empty. Is this normal behavior? Can I return "as before" when the field, having received focus, is filled with the current datetime value at the current moment? I know about (default) in the model, but at the time of creation I don’t need to fill in the field, it should be empty - I need the field to be filled with the current date when it receives focus

Avatar
Discard
Best Answer

This is my solution in Odoo 18.0. I think it same on 17.0

/** @odoo-module **/

import { DateTimeField, dateRangeField } from "@web/views/fields/datetime/datetime_field";

import { patch } from "@web/core/utils/patch";

import { onMounted, useRef } from "@odoo/owl";

const { DateTime } = luxon;


patch(DateTimeField.prototype, {

setup() {

super.setup();

this.startDateRef = useRef("start-date");


onMounted(() => {

this.startDateRef.el.addEventListener("focus", this.onFocus.bind(this));

});

},


/**

* @param {PointerEvent} ev

*/

onFocus ({ target }) {

if(this.props.defaultStartDateField === "today" && !this.state.value[0]) {

this.state.value = [

DateTime.now(),

false

];

}

},

});


DateTimeField.props = {

...DateTimeField.props,

defaultStartDateField: { type: String, optional: true },

};


const super_extractProps = dateRangeField.extractProps;

dateRangeField.extractProps = ({ attrs, options }, dynamicInfo) => ({

...super_extractProps({ attrs, options }, dynamicInfo),

defaultStartDateField: options.default_start_date_field,

});



In the views, you can add it to options
<field name="date_start" position="attributes">

<attribute name="options">{'end_date_field': 'date_end', 'useCurrent': 'day'}</attribute>

</field>

Avatar
Discard
Related Posts Replies Views Activity
0
Oct 24
944
2
Feb 22
2501
1
May 24
1915
1
Jun 23
3778
0
Apr 22
4493