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

hi all, in a Model I have to define a field to get time only (get current system time with onchange or depends method) but it can be modifiable by user in case he/she wants to enter a different time, but a format mask should restrict to enter a valid time or i can check and warn and don't save the data.

OR

i have to change field type from Datetime to Char or Float etc. to calculate them?

regards

Avatar
Discard
Best Answer

Hi, 

Install the following module:

https://apps.odoo.com/apps/modules/11.0/web_widget_timepicker/

and follow the following code:

ofc_in_time = fields.Float(string="Office In Time", digits=(12, 2), copy=False)

<field name="ofc_in_time" widget="timepicker"/>


Thanks

Avatar
Discard
Author

anything else without installing another module? here is my sample Model:

from odoo import models, fields, api

from datetime import datetime

class MyModel(models.Model):

_name = 'xyz.mymodel'

_description = "My Testings..."

name = fields.Char(string="Name here...")

time = fields.Datetime(string='Time', compute="_compute_time")

@api.onchange('start_date')

def _compute_time(self):

now = datetime.now()

final_time = now.strftime("%X")

self.time = final_time

in xml: <field name="time" widget="float_time"/>

also tried to have a Char type field which showing NaN:NaN instead of valid time, for Datetime type field it is showing following error while refereshing the page :

ValueError: time data '07:27:07 AM' does not match format '%Y-%m-%d '

You can also use:

ofc_in_time = fields.Float(string="Office In Time", digits=(12, 2), copy=False)

<field name="ofc_in_time" widget="float_time"/>

Related Posts Replies Views Activity
1
Feb 21
8277
2
Mar 24
863
1
Jul 22
1907
0
Oct 21
3074
3
Jul 21
4845