调用delete()时,我在Django 2.0.3中得到'模型尚未加载'错误

时间:2018-03-29 16:03:26

标签: python django django-2.0

我在一个名为coins的应用程序中定义了两个模型。

models.py
from django.db import models

class Coin(models.Model):
   model defintions

class CoinData(models.Model):
     model defintions

我有一个函数可以使用for循环将数据写入我的Coin模型(这在models.py中)

models.py
for each_item in list:
 each_item = Coin(name=each_item)

这样可以正常工作,但每次我写入模型时,即使已存在一个实例,它也会创建一个额外的实例。当我优化我的代码时,我只想删除Coin中的所有实例。列表是1500+,所以我无法从Admin中删除(这确实有效,但如果我尝试一次删除所有内容就会产生错误)。

所以我在Coins中的对象上运行了delete()函数的许多变体:

Coin.objects.all().delete()

或者

for x in Coin.objects.all()
 x.delete()

只有当我调用delete()时才会出现错误

    File "C:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\Anaconda\lib\site-packages\django\core\management\__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Anaconda\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Anaconda\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Anaconda\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Anaconda\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Anaconda\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\mypython\crypt\coins\models.py", line 31, in <module>
    Coin.objects.all().delete()
  File "C:\Anaconda\lib\site-packages\django\db\models\query.py", line 661, in delete
    collector.collect(del_query)
  File "C:\Anaconda\lib\site-packages\django\db\models\deletion.py", line 186, in collect
    if self.can_fast_delete(objs):
  File "C:\Anaconda\lib\site-packages\django\db\models\deletion.py", line 146, in can_fast_delete
    for related in get_candidate_relations_to_delete(opts):
  File "C:\Anaconda\lib\site-packages\django\db\models\deletion.py", line 58, in get_candidate_relations_to_delete
    f for f in opts.get_fields(include_hidden=True)
  File "C:\Anaconda\lib\site-packages\django\db\models\options.py", line 735, in get_fields
    return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
  File "C:\Anaconda\lib\site-packages\django\db\models\options.py", line 797, in _get_fields
    all_fields = self._relation_tree
  File "C:\Anaconda\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Anaconda\lib\site-packages\django\db\models\options.py", line 708, in _relation_tree
    return self._populate_directed_relation_graph()
  File "C:\Anaconda\lib\site-packages\django\db\models\options.py", line 679, in _populate_directed_relation_graph
    all_models = self.apps.get_models(include_auto_created=True)
  File "C:\Anaconda\lib\site-packages\django\apps\registry.py", line 170, in get_models
    self.check_models_ready()
  File "C:\Anaconda\lib\site-packages\django\apps\registry.py", line 132, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

我的对象上的其他方法,例如all()和save()似乎工作正常吗?

2 个答案:

答案 0 :(得分:2)

您的models.py不应包含在加载模块时运行查询的代码。这包括其他查询,例如Coin.objects.all()coin.save()(即使它们还没有导致您出现错误)。查看回溯,您可以看到在删除时处理相关对象的代码会导致错误。

我会从您的模型中移除delete()来电,并在python manage.py shell中运行:

from coins.models import Coin
Coin.objects.all().delete()

另一种选择是创建一个management command,以便您可以运行python manage.py delete_coins,但在这种情况下这样做太过分了。

作为开发过程中的黑客攻击,您可以将delete放在AppConfig.ready()方法中,但请注意文档会特别警告这一点。

答案 1 :(得分:0)

在Django 1.8上尝试使用独立脚本删除所有对象时遇到类似的问题:

Traceback (most recent call last):
  File "import_data.py", line 368, in <module>
    import_data_2018(workbook)
  File "import_data.py", line 283, in import_data_2018
    Estates2018.objects.all().delete()
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/query.py", line 536, in delete
    collector.collect(del_query)
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 193, in collect
    if self.can_fast_delete(objs):
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 155, in can_fast_delete
    for related in get_candidate_relations_to_delete(opts):
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 67, in <genexpr>
    f for f in candidate_model_fields
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/deletion.py", line 62, in <genexpr>
    opts.get_fields(include_hidden=True) for opts in candidate_models
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/options.py", line 740, in get_fields
    return self._get_fields(include_parents=include_parents, include_hidden=include_hidden)
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/options.py", line 802, in _get_fields
    all_fields = self._relation_tree
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/options.py", line 709, in _relation_tree
    return self._populate_directed_relation_graph()
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/db/models/options.py", line 681, in _populate_directed_relation_graph
    all_models = self.apps.get_models(include_auto_created=True)
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File "/home/joao/.ve/estates/local/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

基于此issue,我们已设法对其进行修复,并在脚本中添加了以下内容:

from django import setup as django_setup

django_setup()  # Call it before running anything else on your script

希望这可以帮助遇到相同问题的任何人