Home > Blockchain >  Query Django data base to find a column with a specific value and update that column value
Query Django data base to find a column with a specific value and update that column value

Time:07-12

I have tried very hard to understand how to update my data base, but struggling to even print out the value of the data returned.

My code in views.py:

   #SET THE PLACEHOLDER DATE AND TIME AS A STRING AND CONVERT TO DATETIME
   #QUERY THE DATA BASE TO FIND THE ROW WHERE END_END_TIME = PLACEHOLDER DATE AND TIME
   #OUTPUT THE DATA TO THE TERMINAL
   #UPDATE THE END_DATE_TIME TO CURRENT DATE AND TIME   

   date_time_placeholder = "2023-01-01 12:00:00"
   datetime.strptime(date_time_placeholder, "%Y-%m-%d %H:%M:%S").date()
   get_value = logTimes.objects.get(end_date_time = date_time_placeholder)
   print(get_value)

The output:

logTimes object (155)

I can see in the admin site the code is working, it is finding the correct row, but I am not sure how to print the column data to the terminal instead of the just the object and ID.

What I am trying to achieve ultimately is to update the end_date_time in this row to the current date and time using datetime.now(), I am not having any success, not for the lack of trying for hours. Any help is appreciated.

CodePudding user response:

You are getting the model object but not printing any of the model fields, which is why you are just seeing the object and ID. You can get the field by just printing get_value.end_date_time - if you then want to update it then you can do something like this, Django has a timezone module which I would recommend using:

from django.utils import timezone

get_value.end_date_time = timezone.now()
get_value.save()
  • Related