Обновление строки в базе данных 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')

В моем View у меня есть функция для добавления строки данных при отправке одной из двух форм, которая будет либо добавлять новую строку данных, либо обновлять существующую строку данных. Я не могу понять, как сделать обновление Я перепробовал слишком много вещей, чтобы писать здесь. Я просматривал документацию 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.

Спасибо!

#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')
Вернуться на верх