DatePicker - Set Default Value w/o User

Hey Everyone,

What I’m trying to do: Set default field value.

What I’ve tried and what’s not working: Currently, I have a Form with a DatePicker field. On the code side, my field is as follows:

import datetime
//
//
//
self.add_revenue_date.date = datetime.date.today()

When my Form pops up, the default field value is set per the above code. HOWEVER, if the user (me) doesn’t physically click on the DatePicker field and select a date, whether that’s today() or any other date, the field value does not save to the binded table. Thus, I wanted to know if there’s another snippet of code I can add that would resolve this issue?

Thank you in advance for any advice.

Adam

This might be useful: Why no refresh_data_bindings() for write back data binding? - #25 It uses the Anvil Extras library, and adds in a way to trigger the writeback programmatically. Not sure if it works for date pickers or not.

Another option would be to set the self.item value instead of the date picker value. You have data binding in place, so the date picker would be updated automatically when you update the corresponding self.item entry.

@jshaffstall Thank you for the response. I had a look at your link, and I would like to use the latter option. Can you provide example syntax for setting the self.item value?

I can’t provide exact code, since I don’t know what your data binding looks like. But, in general, if you have the data binding for your date picker to something like self.item['date'], then you would set that value:

self.item['date'] = somenewdatevalue

You might need to call self.refresh_data_bindings() after that to trigger the date picker to refresh, I can’t remember if it triggers in that situation or not.

@jshaffstall Thank you!

1 Like