Hi all. I’m following the Talk Python Anvil training course, and I’ve hit a problem.
What I’m trying to do:
I have a form that allows a user to enter some details (their weight and resting heart rate, and the date that the measurement was taken) called AddMeasurementComponent
. The component has a save button that invokes a server module to add the details the user has entered into a data table.
NB: this forum post appears to be the same issue, although I think I’m already doing the thing they did to resolve it.
What I’ve tried and what’s not working:
As far as I can tell, my code is the same as that of the course (samples below).
When the function to save the data runs, I get:
anvil.tables.TableError: Column 'CreatedDate' is a date - cannot set it to a datetime
As the error suggests, the column in the data table is of the type date
. The error seems to be saying that my function is passing a datetime
object. The full code is below, but the line where I set the date is self.date = self.date_of_measurement.date
.
self.date_of_measurement
is a standard date picker component from the Anvil toolbox.
Note that the debug print statement print(type(self.date))
returns <class 'datetime.date'>
Code Sample:
This code executes when the save button is clicked:
def button_save_click(self, **event_args):
self.label_error_msg.visible = False
error = self.sync_data()
if error:
self.label_error_msg.text = error
self.label_error_msg.visible = True
return
print(type(self.date))
# print("Would have saved the measurement: {} {} {}".format(self.weight, self.rate,
# self.date))
anvil.server.call('add_measurement',self.date, self.weight, self.rate)
navigation.go_home()
The code that sets the date (it’s in the try..except
block):
def sync_data(self):
if not self.text_box_rate.text:
return "Heart rate is required."
if not self.text_box_weight.text:
return "Weight is required."
if not self.date_of_measurement.date:
return "A measurement date is required."
try:
self.weight = int(self.text_box_weight.text)
self.rate = int(self.text_box_rate.text)
self.date = self.date_of_measurement.date
except TypeError as te:
return "Invalid Format: Could not convert data"
except Exception as x:
return "Unknown error: {}".format(x)
return None
Clone link:
clone link