Exception thread in Accessing sqlite3 in django

Issue

I am playing in Django, in views – print('datatable', Datatable.objects.all())
shows this error/exception

Exception in thread Thread-5:
Traceback (most recent call last):
  File "C:\Users\Karthiyayini Dhillip\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Karthiyayini Dhillip\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Karthiyayini Dhillip\Documents\dj\project1\cv\views.py", line 87, in update
    print('datatable', Datatable.objects.all())
  File "C:\Users\Karthiyayini Dhillip\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 255, in __repr__
    return '<%s %r>' % (self.__class__.__name__, data)

models.py

from django.db import models
# Create your models here.

class Users(models.Model):
    email = models.EmailField(max_length = 254)    
    password = models.CharField(max_length=100)

    def __str__(self):
        return(self.email)


class Datatable(models.Model):
    classes = models.CharField(max_length = 254)    
    date_d = models.DateField()

    def __str__(self):
        return(self.date_d, self.classes)

How to solve this, thanks

Solution

The last line of error :

File "C:\Users\Karthiyayini Dhillip\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 255, in __repr__
return '<%s %r>' % (self.__class__.__name__, data)

Told you that the str method throw the error, obviously because you did not return a String object.

Try to return a string instead (i use python f string ) :

def __str__(self):
    return(f"{self.date_d} - {self.classes}")

Answered By – Rvector

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published