Post bootcamp, I didn't even think I could get back into programming. I wasn't confident getting a real programming job, but thankfully, a position opened up at my school for an instructor. I spend a lot of time nagging students about completing their work, but I also get to see their submissions and help them out when they're stuck. This has given me the opportunity to play around with their code without the stress of deadlines. Especially now that I'm handling the Python project. I feel extremely grateful for this opportunity, but I am well aware that I have a long way to go.
A student was looking into using the timefield for his model and wanted to calculate the difference between the 2 user inputs and save it. He later changed his mind about it, but it gave me the opportunity to look into how this can be done. I took a long time to figure this out, but I'm pleased I did :)
form = MyForm(request.POST or None)
if form.is_valid():
updatedForm = form.save(commit=False) #allows us to make changes to the user input
starttime = updatedForm.Start_Time
endtime = updatedForm.End_Time
date = updatedForm.Date
startdatetime = datetime.datetime.combine(date, starttime)
enddatetime = datetime.datetime.combine(date, endtime)
delta_timediff = (enddatetime - startdatetime) #creates a DateTimeDelta instance
timediff = datetime.datetime.strptime(str(delta_timediff), "%H:%M:%S")
updatedForm.Time_Taken = timediff
updatedForm.save()
Comments
Post a Comment