Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shows "Unable to get repr for <class 'django.db.models.query.QuerySet'>" while retrieving data

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 allStudent
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.

like image 512
Shubho Shaha Avatar asked Jun 05 '17 18:06

Shubho Shaha


1 Answers

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:

  1. This problem might arise if you try to filter or access a field that does not exist in your model declaration.
  2. Dirty Model Migration :P : As a beginner this was my nightmare. And most of the time I got this error for improper migration. So in case if you haven't migrated your model changes properly then you will get this error for sure. And to fix this problem you have to reset migration. To do this you can check the following link: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html

Note: I'll keep this answer updated once I get new scenario and their solution.

like image 157
Shubho Shaha Avatar answered Sep 25 '22 02:09

Shubho Shaha