根据列值更新Django数据库中的一行
我是Django的新手,特别是模型,并且一直在努力了解如何更新数据库中的数据。
Models.py:
# Create your models here.
class logTimes(models.Model):
fast_finshed = models.BooleanField(default=False)
start_date_time = models.DateTimeField('start fast')
end_date_time = models.DateTimeField('end fast')
在我看来,在提交两种形式之一时,我有一个函数可以添加一排数据,该表格将添加新的数据行或更新现有的数据行。我不知道如何进行更新,我尝试了太多的内容要在这里写。我一直在审查Django文档,但根本没有取得进展。
Views.py:
#function to add a fast to the data base or update the last fast with an end date and time
def start_or_end_fast(request):
#If starting fast, update the data base with fast_finished = False
#A new fast start date and time using current date and time
#A specific end date and time set in the future
if request.method == 'POST' and 'start_fast' in request.POST:
l = logTimes(fast_finshed=False,start_date_time=datetime.now(),end_date_time=datetime(year=2023, month=1, day=1, hour=12, minute=00, second=00))
l.save()
print('fast started')
return render(request,'startandstoptimes/index.html')
#If ending a fast, find the last fast that has the specific end date and time
#And update end_date_time to the current date and time
elif request.method == 'POST' and 'end_fast' in request.POST:
#Update row where end_date_time = year=2023, month=1, day=1, hour=12, minute=00, second=00
#Change to current date and time
???????
print('Fast ended')
return render(request,'startandstoptimes/index.html')
#If Just refreshing the page
else:
return render(request,'startandstoptimes/index.html')
任何建议都将不胜感激,我发现从使用SQL的情况下,我很难将头缠住。
谢谢你!
I am new to Django and specifically Models and have been struggling to understand how to update data in the data base.
Models.py:
# Create your models here.
class logTimes(models.Model):
fast_finshed = models.BooleanField(default=False)
start_date_time = models.DateTimeField('start fast')
end_date_time = models.DateTimeField('end fast')
In my View, I have a function to add a row of data when submitting one of two forms that will either add a new row of data or update an existing row of data. I cannot figure out how to make the update I have tried too many things to write here. I have been reviewing the Django docs and not making progress at all.
Views.py:
#function to add a fast to the data base or update the last fast with an end date and time
def start_or_end_fast(request):
#If starting fast, update the data base with fast_finished = False
#A new fast start date and time using current date and time
#A specific end date and time set in the future
if request.method == 'POST' and 'start_fast' in request.POST:
l = logTimes(fast_finshed=False,start_date_time=datetime.now(),end_date_time=datetime(year=2023, month=1, day=1, hour=12, minute=00, second=00))
l.save()
print('fast started')
return render(request,'startandstoptimes/index.html')
#If ending a fast, find the last fast that has the specific end date and time
#And update end_date_time to the current date and time
elif request.method == 'POST' and 'end_fast' in request.POST:
#Update row where end_date_time = year=2023, month=1, day=1, hour=12, minute=00, second=00
#Change to current date and time
???????
print('Fast ended')
return render(request,'startandstoptimes/index.html')
#If Just refreshing the page
else:
return render(request,'startandstoptimes/index.html')
Any advice would be much appreciated, I find this a bit difficult to wrap my head around coming from using SQL.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)