Reputation: 747
I have a view where when it call an api, it will save the result get from it.
But i want to be able to change the time get from Post method to like +30min or +1hr and save it.
In my case, there is a starttime
and endtime
. But there is only a field time
given back. So i will save the time
into starttime
and time
+30min/1hr save into endtime
How do i do it ?
views
@csrf_exempt
def my_django_view(request):
if request.method == 'POST':
r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
else:
r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)
if r.status_code == 201 and request.method == 'POST':
data = r.json()
print(data)
# Create a schedule/Save to schedule table
user = data['patientId']
userId = MyUser.objects.get(userId=user)
# time
gettime = data['time']
gettime_add = ???
saveget_attrs2 = {
"userId ": userId ,
"starttime": data["time"],
"date": data["date"],
}
saving2 = Schedule.objects.create(**saveget_attrs2)
Upvotes: 1
Views: 442
Reputation: 8907
First convert datestring to a datetime
object and then use a timedelta
.
import datetime
...
gettime = data['time']
date_obj = datetime.datetime.strptime(gettime, <format_string>)
# for example datetime.datetime.strptime('2017-12-01, '%Y-%m-%d')
end_time = date_obj + datetime.timedelta(minutes=30)
Upvotes: 3