Form datetime fields and default values

63 views
Skip to first unread message

Alan Etkin

unread,
Feb 12, 2026, 6:10:36 AMFeb 12
to py4web
Hi everyone

I'm using the datetime field in a table of a py4web project, but when I
specify a default value with

db.table.field.default = <datetime value>

The value doesn't show properly in the browser, instead, the field value remains
not defined (as if waiting for input).

I'm not sure this is about my app code or this is the actual behavior.

The controller

# Restrict fields for different roles
db.task.status.default = task.status or "pending"
db.log.title.comment = "Name the task report"
db.log.body.comment = "Write some comment on the progress"
db.task.end.default = now
db.task.end.comment="Only applies for done status; defaults to current time"

# Autopopulate form with task status and other stuff

form = Form([db.task.status,
db.task.end,
db.log.title,
db.log.body,
db.log.tags])

Form is composed of fields from tables in the model

Checking the source code in the browser it shows

<input class="" id="task_end" name="end" type="datetime-local" value="2026-02-12 07:51:53.598215"/>

The default value is placed in the html render but it seems that the js widget does not like it

Regards

Alan

Julio Flores Schwarzbeck

unread,
Feb 12, 2026, 11:16:20 PMFeb 12
to py4web
What worked for me in my model I've added a 'now' function

def now():
return datetime.datetime.now(datetime.timezone.utc)

And (in my case, further down..)
...
Field('modified_on', type='datetime', default=now, update=now),
...

Alan Etkin

unread,
Feb 19, 2026, 8:38:21 AM (12 days ago) Feb 19
to py4web
Thanks! I'll check using a function as argument

Alan Etkin

unread,
Feb 23, 2026, 5:23:11 PM (8 days ago) Feb 23
to py4web
Folks, I checked your solution but could not solve my case. Here's the thing:

If you set a .default datetime field value using a value stored in a Row object, it (AFAIK) is set with no microsecond property
But if you are using something like datetime.now() you will most likely store a value with microsecond different from zero

With the second case, the browser wont show the default value properly in the widget, even if at html tag the default value will be present.

Regards

Alan

Ali

unread,
Feb 24, 2026, 7:48:27 PM (7 days ago) Feb 24
to py4web
Alan,

I faced a similar issue earlier today. Good catch; it looks like it has to do with the microseconds.

Here is a possible solution: manually add the IS_DATETIME() requirement to the fields of type datetime.

- ali
Reply all
Reply to author
Forward
0 new messages