0

I have a self cache definition in my models and I want to clear old caches when server is restarting.

I use this code in settings.py after INSTALLED_APPS:

from cache.models import Cache
Cache.clear()

And in Cache/models.py:

class Cache(models.Model):
    key = models.CharField("key", max_length=511, db_index=True)
    data = models.TextField("data")
    expiration = models.DateTimeField("expiration", db_index=True, null=True)

    @staticmethod
    def clear():
        Cache.objects.all().delete()

But I get this error:

raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
mrash
  • 883
  • 7
  • 18
  • The settings file is the first that gets interpreted before even loading of the models file. You need to make sure that you don't call any thing related to the models file in your settings file. – Muhammad Shoaib Apr 12 '16 at 12:07
  • I want to use my models in `Cache.cleare()` – mrash Apr 12 '16 at 12:18
  • I use `nginx` and `uwsgi` and server restarting automatically. I can't use command and manage.py – mrash Apr 12 '16 at 12:33

0 Answers0