Home > front end >  Update a row in a Django data base based on a column value
Update a row in a Django data base based on a column value

Time:07-07

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!

CodePudding user response:

#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')
  • Related