根据列值更新Django数据库中的一行

发布于 2025-02-13 18:15:05 字数 1666 浏览 1 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别挽留 2025-02-20 18:15:05
#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 
    logTimes.objects.filter(
        # this will be the WHERE part of the SQL query
        end_date_time=datetime(year=2023, month=1, day=1, hour=12, minute=0, second=0)
    ).update(
        # this will be the SET XXX VALUES XXX part of the query
        end_date_time=datetime.now()
    )
   
    print('Fast ended')
    return render(request,'startandstoptimes/index.html')

#If Just refreshing the page
 else:
    return render(request,'startandstoptimes/index.html')
#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 
    logTimes.objects.filter(
        # this will be the WHERE part of the SQL query
        end_date_time=datetime(year=2023, month=1, day=1, hour=12, minute=0, second=0)
    ).update(
        # this will be the SET XXX VALUES XXX part of the query
        end_date_time=datetime.now()
    )
   
    print('Fast ended')
    return render(request,'startandstoptimes/index.html')

#If Just refreshing the page
 else:
    return render(request,'startandstoptimes/index.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文