Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-activity-stream : Apps aren't loaded yet

Tags:

python

django

I try to use the Django-activity-stream package but I have a problem with the configuration (http://django-activity-stream.readthedocs.org/en/latest/configuration.html).

The name of my project is proj and I want use this package on the main_app activity.

/proj

/proj /main_app

So in main_app/apps.py I did this :

from django.apps import AppConfig
from actstream import registry


class MainAppConfig(AppConfig):
    name = 'main_app'

    def ready(self):
        registry.register(self.get_model('Car'))

But I have a error when I add this line in main_app/init.py :

default_app_config = 'main_app.apps.MainAppConfig'

The error :

  Traceback (most recent call last):
  File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
 File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 176, in fetch_command
commands = get_commands()
     File "/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 100, in wrapper
result = user_function(*args, **kwds)
     File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 71, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
     File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 137, in get_app_configs
   self.check_apps_ready()
        File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 124, in check_apps_ready

 raise AppRegistryNotReady("Apps aren't loaded yet.")
 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

My settings :

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'django.contrib.sites',
    'main_app',
    'actstream',
]

Edit (2) :

Now I have this in main_app/apps.py :

from django.apps import AppConfig

class MainAppConfig(AppConfig):
    name = 'main_app'

def ready(self):
    from actstream import registry
    registry.register(self.get_model('Car'))
like image 274
Zoulou Avatar asked Feb 19 '16 15:02

Zoulou


1 Answers

Django is not ready yet when you run against it. You should set the environment variable, and setup django (which usually runs when wsgi loads the django application):

import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
django.setup()

from django.apps import AppConfig

class MainAppConfig(AppConfig):
    name = 'main_app'

    def ready(self):
        from actstream import registry
        registry.register(self.get_model('Car'))
like image 89
Aviah Laor Avatar answered Sep 20 '22 05:09

Aviah Laor