2

I'm trying to send data to my database using Django Models and Views file but I'm continuously getting this error: "raise self.model.DoesNotExist( crm.models.Time_Stamps.DoesNotExist: Time_Stamps matching query does not exist."

I do have 'Time_Stamps' as a Model in models.py file.

How to resolve it?

views.py

def update_counter(request):
    if request.method == 'POST':
        timestamp = Time_Stamps.objects.get()
        counterValue = request.POST['counter']
        #timestamp.Task_ID = counterValue
        timestamp.startTime = request.POST['timestamp']
        print(counterValue)
        print(startTime)
        timestamp.save()
        message = 'update successful'
    return HttpResponse(message)

models.py

class Time_Stamps(models.Model):
    TimeStamp = models.IntegerField(default=0)
    def __str__(self):
        return self.TimeStamp
    class Meta:
        verbose_name_plural = 'TimeStamps'

ERROR

......................
File "C:\Users\kushw\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 435, in get
    raise self.model.DoesNotExist(
crm.models.Time_Stamps.DoesNotExist: Time_Stamps matching query does not exist.
[24/Sep/2021 15:44:27] "POST /update_counter/ HTTP/1.1" 500 73227
  • `Time_Stamps.objects.get()` is throwing exception due to no record in database. I can guide you if you can let me know what you are trying to achieve here. – kamran890 Sep 24 '21 at 10:36
  • Ok, so I'm trying to store a JavaScript variable in the database using Ajax which is in one of my HTML files. – Kushagra A. Nalwaya Sep 24 '21 at 10:39
  • That variable is `timestamp`? – kamran890 Sep 24 '21 at 10:42
  • What is the purpose of using `Time_Stamps.objects.get()`? – kamran890 Sep 24 '21 at 10:42
  • Yes, it is supposed to be an actual "Timestamp" variable but as of now it's just an integer. Actually I'm trying to something like this: https://stackoverflow.com/questions/49500189/django-how-to-save-javascript-variable-into-django-database – Kushagra A. Nalwaya Sep 24 '21 at 10:45
  • I'm using "Time_Stamps.objects.get()" to give it a reference (I guess). Actually I'm new in Django and I just tried copying the solution from that previous link. – Kushagra A. Nalwaya Sep 24 '21 at 10:52
  • 2
    You need to read Django documentation regarding how to make queries: https://docs.djangoproject.com/en/3.2/topics/db/queries/. `Time_Stamps.objects.get()` will work only if there is only one record in database, otherwise it will throw exception. – kamran890 Sep 24 '21 at 10:55
  • Thanks a lot it worked. :) Instead of using "timestamp = Time_Stamps.objects.get()", I need to use "timestamp = Time_Stamps(TimeStamp='SomeValue'). – Kushagra A. Nalwaya Sep 24 '21 at 11:00

0 Answers0