Skip to main content
 首页 » 编程设计

"" rel="bookmark">python之检索数据时显示 "Unable to get repr for class ' django.db.models.query.QuerySet'>"

2025年05月04日41grandyang

我一直在尝试从模型 StudentInfo 中检索所有数据。 但它显示以下错误。

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

调试我的代码后,我发现导致错误的行是

allStudent = StudentInfo.objects.all()

调试信息显示:

Unable to get repr for class 'django.db.models.query.QuerySet'

这是我的模型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) 

以及与StudentInfo相关的另一个模型BatchInfo

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 

奇怪的是,我在其他地方使用了相同风格的代码,但它们运行良好。

all_batch = BatchInfo.objects.all()

我尽力自己解决,但作为新手,我发现这对我来说非常困难。所以我请求你的帮助。 提前致谢。

请您参考如下方法:

到目前为止,我对这个问题的了解是,对于这种特定类型的问题,不存在明确的答案。 Django 出于多种原因显示此错误。所以我将列出到目前为止我遇到的所有场景和解决方案:

  1. 如果您尝试过滤或访问模型声明中不存在的字段,则可能会出现此问题。
  2. 脏模型迁移 :P : 作为初学者,这是我的噩梦。大多数时候我因为不正确的迁移而收到此错误。因此,如果您没有正确迁移模型更改,那么您肯定会收到此错误。要解决此问题,您必须重置迁移。为此,您可以查看以下链接: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html

注意:一旦我得到新的场景和他们的解决方案,我会及时更新这个答案。