簡體   English   中英

Django shell 無法執行 ORM 查詢

[英]Django shell not able to execute ORM query

我通過使用命令python manage.py shell將 Django 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:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.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:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 250, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 274, in __iter__
    self._fetch_all()
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1100, in execute_sql
    cursor.execute(sql, params)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute
    return super().execute(sql, params)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "D:\production\siralo\siralo_py3\venv\lib\site-packages\django\db\backends\utils.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 中也遇到了同樣的錯誤,而我還沒有運行下面的遷移命令是錯誤的屏幕截圖我得到這是相同錯誤的屏幕截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM