【发布时间】:2021-01-16 07:47:04
【问题描述】:
我有一个简单的模型类如下
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
【问题讨论】:
-
对于尚未进行任何迁移的新应用,您需要将应用名称传递给
makemigrations以创建初始迁移文件:python manage.py makemigrations home。然后运行python manage.py migrate -
尝试将类名更改为
language,然后使用llist = language.objects.filter(),也不要忘记再次makemigrations和migrate -
我尝试了上面的建议,但是这个错误发生在迁移之前,所以它不起作用
-
@Knight“这个错误发生在迁移之前”:你的意思是你在尝试创建迁移时遇到错误吗?如果是这样,您需要移动此代码,使其不会在您的应用启动时执行
-
@Knight 此代码在您的 models.py 文件中,在加载文件时执行查询,迁移将加载导致错误的文件。从您的 models.py 中删除代码,您应该能够迁移。为什么它首先位于该文件的根目录中?