I’ve been trying to retrieve all data from model StudentInfo.
But it shows the following error.
django.db.utils.ProgrammingError: column student_studentinfo.gurdians_mobile does not exist
Error in formatting: ProgrammingError: column student_studentinfo.gurdians_mobile does not exist
LINE 1: …ile_no1″, “student_studentinfo”.”current_address”, “student_s…
After debugging my code, I found the line that causes error is
allStudent = StudentInfo.objects.all()
And the debugging messages Shows:
Unable to get repr for class ‘django.db.models.query.QuerySet’
Here is my model StudentInfo
class StudentInfo(models.Model): student_name = models.CharField("Student Name",max_length=20) admission_date = models.DateField("Admission Date") mobile_no1 = models.CharField("Mobile No.",max_length=12) current_address = models.CharField("Current Address",max_length=20,blank=True) batch_number = models.ForeignKey(BatchInfo) coaching_id = models.IntegerField("Coaching ID") def __unicode__(self): return self.student_name def __repr__(self): return str(self.student_name)
And the other model BatchInfo that is related to StudentInfo
class BatchInfo(models.Model): batch_name = models.CharField("Batch Name",max_length=20) batch_started_from = models.DateField("Batch Started From") no_of_registered_student = models.IntegerField("Number of Registered Student so far",default=0) def __str__(self): return self.batch_name def __unicode__(self): return self.batch_name
The strange part is that I’ve used the same style of code in other places which are perfectly functioning.
all_batch = BatchInfo.objects.all()
I try my best to solve it by my own but as a newbie, I found it very difficult for me. So I ask your help.
Thanks in advance.
It looks like you are using Python 3.x
So what I’ve learned so far about this problem is that there is no definitive answer exists for this particular type of problem. Django shows this error for multiple reasons. So I’m going to list all the scenarios and solutions that I’ve came across so far for this problem:
Note: I’ll keep this answer updated once I get new scenario and their solution.
These problems arise when you have a problem with your DB.
Very excellent answer by @Shubho Shaha. I encounter the same problem and I debug it by following method.
class G(models.Model): subscription_startdate = models.DateField(default=timezone.datetime.today().date()) ... ...
I tried to access each field separately using
.values and I found a problem with my DateField column because I assign wrong default value
I faced this problem due to bad variable naming. In my case:
class SomeModel(models.Model): User = models.ForeignKey(User, .....) # other fields ... # . # . # .
The issue here is that I named the field user with capital ‘U’, i-e "User", which is actually the name of my django custom User Model, which, for some reason, django didn’t like that and and got angry :p.
So I simply made the user field lower case, i-e :
user = models.ForeignKey(User, .....) # User --> user
My solution was simple.
class Report(TimestampedModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) report_id = models.CharField(max_length=255, blank=True, null=True) sector = models.CharField(max_length=255, blank=True, null=True) def __str__(self): return self.project_name
What caused my issue was that I was using a field (project_name) that was not part of the model fields. So I changed it to what was in the model and I stopped getting the error. .
def __str__(self): return self.sector
This is how my problem got fixed.
Another cause for the above issue as I found out here is that the returned data from the def str(self) method should be a string. Anything other than that will throw the above error.
A fix can be to convert the data type of what youre returning to a string like this;
def __str__(self): return str(self.date_answered)
This can happen when you do not specify the model name correctly in the serilizer. In my case this was the mistaken, and when i corrected it, it worked. here is an example;
class ResourcesChosen(models.Model): resources = models.ForeignKey(Resource, null=True, default=None, on_delete=models.CASCADE) poll = models.ForeignKey(Polls, null=True, default=None, on_delete=models.CASCADE) appusers = models.ForeignKey(AppUser, null=True, default=None, on_delete=models.CASCADE) farmers = models.ForeignKey(Farmer, null=True, default=None, on_delete=models.CASCADE) longitude = models.DecimalField(max_digits=11, decimal_places=7, null=True, blank=True) latitude = models.DecimalField(max_digits=11, decimal_places=7, null=True, blank=True) def __str__(self): return self.resources
class ResourcesChosenSerializer(serializers.ModelSerializer): class Meta: model = Replies fields = ( 'id', 'resources' 'poll' 'appusers', 'farmers', 'latitude', 'longitude', )
As you can see, the model name is different from what i specified in the model and that was causing issues. The correct way is;
class ResourcesChosenSerializer(serializers.ModelSerializer): class Meta: model = ResourcesChosen fields = ( 'id', 'resources' 'poll' 'appusers', 'farmers', 'latitude', 'longitude', )
This fix eliminated the problem, "Unable to get repr for <class ‘django.db.models.query.QuerySet’>" while retrieving data