python - 检索数据时显示 "Unable to get repr for <class ' django.db.models.query.QuerySet'>"

标签 python django python-3.x django-models

我一直在尝试从模型 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

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

关于python - 检索数据时显示 "Unable to get repr for <class ' django.db.models.query.QuerySet'>",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44375188/

相关文章:

python - 在 Django 中的抽象父模型类中实现所需的接口(interface)

python - Python 加权搜索和排序

python - 在 web.py 中呈现 HTML

python - Google BigQuery API(Python 客户端库)> 查询数据(异步)

python - 如何使用 tf.keras.layers 通过 Tensorflow conv2d 馈送批量图像序列

python - 逗号分隔列表的 argparse 操作或类型

javascript - Django 在 javascript 中调用 url

django - 如何从 django-cms 2.4 URL 中删除语言标识符?

python - 如何让 if 语句接受三个参数中的任何一个而不在 Python 中全部为真

python-3.x - 在 Elastic Beanstalk 上部署 Flask 应用程序 : No module named 'application'