检索数据时显示“无法获得 <class 'django.db.models.query.QuerySet'> 的 repr”

Shu*_*aha 5 python django django-models python-3.x

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

django.db.utils.ProgrammingError:列 student_studentinfo.gurdians_mobile 不存在 allStudent
格式错误:ProgrammingError:列 student_studentinfo.gurdians_mobile 不存在 LINE 1: ...ile_no1", "student_studentinfo"."current_address", "student_s.. .

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

allStudent = StudentInfo.objects.all()

调试消息显示:

无法获得类“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)
Run Code Online (Sandbox Code Playgroud)

以及与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
Run Code Online (Sandbox Code Playgroud)

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

all_batch = BatchInfo.objects.all()

我尽量自己解决,但作为一个新手,我发现这对我来说很困难。所以我请你帮忙。提前致谢。

Shu*_*aha 7

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

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

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