Reputation: 311
I have two models defined in an app called coins.
models.py
from django.db import models
class Coin(models.Model):
model defintions
class CoinData(models.Model):
model defintions
I have a function which can write data to my Coin model using a for loop (this is inside models.py)
models.py
for each_item in list:
each_item = Coin(name=each_item)
This works fine, but each time I write to the model, it's creating an additional instance even if one already exists. While I refine my code, I simply want to delete all instances in Coin.The list is 1500+ so I cannot delete from Admin (which does work, but produces an error if I try to delete all at once).
So I run many variations of the delete() function on the objects in Coins:
Coin.objects.all().delete()
Or
for x in Coin.objects.all()
x.delete()
It's only when I call delete() that I get the error
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.
Other methods on my objects, such as all() and save() seem to work just fine?
Upvotes: 0
Views: 517
Reputation: 425
Had a similar issue when trying to delete all objects using a standalone script on 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.
Based on this issue I've managed to fix it, adding the following to the script:
from django import setup as django_setup
django_setup() # Call it before running anything else on your script
Hope this helps anyone facing the same issue
Upvotes: 1
Reputation: 309099
Your models.py
should not include code that runs queries when the module is loaded. This includes other queries like Coin.objects.all()
or coin.save()
(even if they didn't cause you an error yet). Looking at the traceback, you can see that it's the code that handles related objects when deleting which leads to the error.
I would remove the delete()
call from your models, and run it in python manage.py shell
:
from coins.models import Coin
Coin.objects.all().delete()
Another option would be to create a management command, so that you could run python manage.py delete_coins
, but that's overkill in this case.
As a hack during development, you could probably put the delete
in your AppConfig.ready()
method, but note that the docs specifically warn against this.
Upvotes: 2