跳至內容
選單
此問題已被標幟
1 回覆
8656 瀏覽次數

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

頭像
捨棄
最佳答案

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

頭像
捨棄
作者

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"/>

相關帖文 回覆 瀏覽次數 活動
1
2月 21
9994
2
3月 24
2421
1
7月 22
2950
0
10月 21
3941
3
7月 21
6077