Django shell无法执行ORM查询



我使用Django shell通过命令python manage.py shell调试我的项目。

我发现我无法从shell执行ORM查询;低于某种错误。

from base.models import Template_Form, Template_Form_Field
Template_Form_Field.objects.all()

在shell中执行以上代码后,我得到了以下错误。

Traceback (most recent call last):
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 84, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "base_template_form_field" does not exist
LINE 1: ...ions", "base_template_form_field"."sequence" FROM "base_temp...
^

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbmodelsquery.py", line 250, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbmodelsquery.py", line 274, in __iter__
self._fetch_all()
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbmodelsquery.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbmodelsquery.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbmodelssqlcompiler.py", line 1100, in execute_sql
cursor.execute(sql, params)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 99, in execute
return super().execute(sql, params)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbutils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:productionsiralosiralo_py3venvlibsite-packagesdjangodbbackendsutils.py", line 84, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "base_template_form_field" does not exist
LINE 1: ...ions", "base_template_form_field"."sequence" FROM "base_temp...

有人知道我为什么会犯以上错误吗;如何摆脱它。

我试过在谷歌上搜索,但找不到任何解决方案。

模板表单字段模型如下。

class Template_Form_Field(models.Model):
template_form = models.ForeignKey(Template_Form, on_delete=models.CASCADE)
name = models.CharField(max_length=40, verbose_name='Name')
caption = models.CharField(max_length=80, verbose_name='Caption')
value_options = models.TextField(null=True, blank=True, verbose_name='Value options')
sequence = models.IntegerField(blank=True, null=True, verbose_name='Sequence')

谢谢。

只运行python manage.py migrate这个错误是因为您刚刚运行了python manage.py-makemigrations,但没有运行python manage.py.migrate。

当我没有运行迁移命令时,我在shell中也遇到了同样的错误。下面是我得到的错误的屏幕截图,这是相同错误的屏幕截图

最新更新