django return Database.Cursor.execute(self,query,params)django.db.utils.OperationalError:无此表:

7bsow1i6  于 2023-08-08  发布在  Go
关注(0)|答案(6)|浏览(125)

我有一个简单的模型类如下

class Language(models.Model):
    name= models.CharField(max_length=20)
    code= models.CharField(max_length=5)
    status=models.BooleanField()
    create_at=models.DateTimeField(auto_now_add=True)
    update_at=models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

字符串
我想这样用它

llist = Language.objects.filter()
list1 = []
for rs in llist:
    list1.append((rs.code,rs.name))
langlist = (list1)


但是当我尝试使用llist时,它总是抛出这个错误:

return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: home_language


我尝试使用python manage.py shell进行查询,它显示相同的上述错误。
这是完整的错误:

Traceback (most recent call last):
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: home_language

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\users\daisy\appdata\local\programs\python\python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Daisy\OneDrive\Documents\Work\django\prjs\funnystore\mysite\home\models.py", line 29, in <module>
    for rs in llist:
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 276, in __iter__
    self._fetch_all()
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\models\sql\compiler.py", line 1152, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Daisy\Downloads\Django-E-Commerce-master\env\lib\site-packages\django\db\backends\sqlite3\base.py", line 396, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: home_language

omjgkv6w

omjgkv6w1#

在命令提示符下使用以下代码:
python manage.py migrate --run-syncdb
这对我很有效……

twh00eeo

twh00eeo2#

我使用了特定应用程序的迁移,在您的情况下,它是:

python3 manage.py makemigrations home
python3 manage.py migrate

字符串

jvlzgdj9

jvlzgdj93#

您应该使用以下方法进行迁移并迁移数据库:

python manage.py makemigrations
python manage.py migrate

字符串

gfttwv5a

gfttwv5a4#

在我的情况下,错误是解决了通过您的管理面板提前删除数据.然后又道:

python manage.py makemigrations
python manage.py migrate

字符串

kognpnkq

kognpnkq5#

You can delete the SQLite db and files in the migration folder and then run the command ....python manage.py makemigrations (your name app)... and then python manage.py migrate.

个字符

qyyhg6bp

qyyhg6bp6#

如果在任何条件下使用了此模型(home_language),请从所有代码中删除它,然后迁移它:

python3 manage.py makemigrations
python3 manage.py migrate

字符串
希望能成功。

相关问题