Alex
Alex

Reputation: 4264

Calling delete() in Django gives error message

Let's say in Django I have the model:

class Test(models.Model):
    name = models.CharField(max_length = 100)

I then use the following code in the Django Shell:

from app.models import Test
test = Test(name = "Hi")
test.save()

So far so good. Now I do:

Test.objects.all().delete()

I then get a very long error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\query.py", line 536, in delete
    collector.collect(del_query)
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\deletion.py", line 193, in collect
    if self.can_fast_delete(objs):
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\deletion.py", line 155, in can_fast_delete
    for related in get_candidate_relations_to_delete(opts):
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\deletion.py", line 67, in <genexpr>
    f for f in candidate_model_fields
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\deletion.py", line 62, in <genexpr>
    opts.get_fields(include_hidden=True) for opts in candidate_models
  File "C:\Users\Alexander\Anaconda3\lib\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 "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\options.py", line 802, in _get_fields
    all_fields = self._relation_tree
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\utils\functional.py", line 59, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Alexander\Anaconda3\lib\site-packages\django\db\models\options.py", line 709, in _relation_tree
    return self._populate_directed_relation_graph()
  File "C:\Users\Alexander\Anaconda3\lib\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 "C:\Users\Alexander\Anaconda3\lib\site-packages\django\apps\registry.py", line 168, in get_models
    self.check_models_ready()
  File "C:\Users\Alexander\Anaconda3\lib\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.

I have seen other people get a similar error message, but they were not using the delete() operation nor were they using the Django shell. If it is relevant, I am using Django 1.8.6 in Microsoft Visual Studio 2015.

Upvotes: 0

Views: 333

Answers (1)

qasimalbaqali
qasimalbaqali

Reputation: 2131

From different SO answers that I've seen, this will do the trick:

import django
django.setup()

and the reason behind it is

Basically, Django has a new way to load installed app. If you load Django from a Python script (like I was in my custom unit tests), some initialization needs to be done before proceeding and calling setup() is how to do it. Aside from that, kudos to the team, my 1.6.2 to 1.7.1 upgrade seems to an hour's worth of real work.

– JL Peyret

Upvotes: 1

Related Questions