是否可以使用django view orm将当前时间+ 1小时或1天或1周保存到DatetimeField中?
例如
models.py
dead_line = models.DateTimeField(blank=True, default=utc_tomorrow)
def add_todo_by_ajax(request):
title = request.POST['title']
dead_line_option = request.POST['dead_line_option']
# print("dead_line_option : " , dead_line_option)
if dead_line_option == "1h":
print ("1h")
# deadline =
elif (dead_line_option == "4h"):
print ("4h")
elif (dead_line_option == "8h"):
print ("8h")
elif (dead_line_option == "1d"):
print ("1d")
elif (dead_line_option == "8h"):
print ("1w")
todo = Todo.objects.create(title=title, author=request.user, director = request.user)
print("todo(insert result) : " , todo)
user_update = Profile.objects.filter(user=request.user.id).update(uncompletecount = F('uncompletecount')+1)
return HttpResponse(redirect('/todo/'))
答案 0 :(得分:0)
这是我找到的答案
if dead_line_option == "1h":
print ("1h")
dead_line = datetime.now() + timedelta(hours=1)
elif (dead_line_option == "4h"):
print ("4h")
dead_line = datetime.now() + timedelta(hours=4)
elif (dead_line_option == "8h"):
print ("8h")
dead_line = datetime.now() + timedelta(hours=8)
elif (dead_line_option == "1d"):
print ("1d")
dead_line = datetime.now() + timedelta(hours=24)
elif (dead_line_option == "1w"):
print ("1w")
dead_line = datetime.now() + timedelta(days=7)