etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.21k stars 129 forks source link

Registering Django FlatPage in apps.py or __init__.py Throws "Apps aren't loaded yet" Exception #280

Closed nicorellius closed 3 years ago

nicorellius commented 4 years ago

Thanks for a great app @etianen. You helped me quite a bit with a few email exchanges, so I wanted to thank you once again.

I've got a comment I wanted to throw out there if anyone else was trying to register Django flat pages with Watson in Django 3.x. Putting the watson.register() code in apps.py or __init__.py doesn't seem to work, and it throws the elusive:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Django chokes on importing the FlatPage model: from django.contrib.flatpages.models import FlatPage

I ended up putting the register code for the flat page model in the models.py file in an app called search instead, which worked (not ideal, but at least it's working):

from watson import search as watson
from django.contrib.flatpages.models import FlatPage

watson.register(FlatPage)

I wasn't sure if I should create a PR or try to edit your wiki page on registering models, as this isn't well tested or completely proven to be true. It's just the way I got it to work.

etianen commented 4 years ago

Putting the watson.register() inside apps.py really should work. Do you have a full traceback?

On Sun, 22 Nov 2020 at 01:34, Nick Vincent-Maloney notifications@github.com wrote:

Thanks for a great app @etianen https://github.com/etianen. You helped me quite a bit with a few email exchanges, so I wanted to thank you once again.

I've got a comment I wanted to throw out there if anyone else was trying to register Django flat pages with Watson in Django 3.x. Putting the watson.register() code in apps.py or init.py doesn't seem to work, and it throws the elusive:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Django chokes on importing the FlatPage model: from django.contrib.flatpages.models import FlatPage

I ended up putting the register code for the flat page model in the models.py file in an app called search instead, which worked (not ideal, but at least it's working):

from watson import search as watson from django.contrib.flatpages.models import FlatPage

watson.register(FlatPage)

I wasn't sure if I should create a PR or try to edit your wiki page on registering models, as this isn't well tested or completely proven to be true. It's just the way I got it to work.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/280, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEKCFND7X2OJLSZLNYYR3SRBTADANCNFSM4T6EXO7A .

nicorellius commented 4 years ago

I've tried putting it in apps.py, __init__.py in different ways, eg, in the SearchConfig class, outside of it below, etc. I've tried various things somewhat irrationally just to see if it'll work, to no avail. Hopefully I'm just doing something silly and you are right.

All give this stacktrace:

  File "./manage.py", line 26, in <module>
    main()
  File "./manage.py", line 22, in main
    execute_from_command_line(sys.argv)
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/core/management/__init__.py", line 377, in execute
    django.setup()
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/nick/dev/psweb/pswebsite/search/apps.py", line 2, in <module>
    from django.contrib.flatpages.models import FlatPage
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/contrib/flatpages/models.py", line 1, in <module>
    from django.contrib.sites.models import Site
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/contrib/sites/models.py", line 78, in <module>
    class Site(models.Model):
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/db/models/base.py", line 108, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config
    self.check_apps_ready()
  File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
etianen commented 3 years ago

Can I also see the code for your apps.py registration that caused this traceback?

On Thu, 26 Nov 2020 at 21:41, Nick Vincent-Maloney notifications@github.com wrote:

I've tried putting it in apps.py, init.py in different ways, eg, in the SearchConfig class, outside of it below, etc. I've tried various things somewhat irrationally just to see if it'll work, to no avail. Hopefully I'm just doing something silly and you are right.

All give this stacktrace:

File "./manage.py", line 26, in main() File "./manage.py", line 22, in main execute_from_command_line(sys.argv) File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/core/management/init.py", line 401, in execute_from_command_line utility.execute() File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/core/management/init.py", line 377, in execute django.setup() File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 91, in populate app_config = AppConfig.create(entry) File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/config.py", line 90, in create module = import_module(entry) File "/usr/local/lib/python3.8/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 961, in _find_and_load_unlocked File "", line 219, in _call_with_frames_removed File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "", line 783, in exec_module File "", line 219, in _call_with_frames_removed File "/home/nick/dev/psweb/pswebsite/search/apps.py", line 2, in from django.contrib.flatpages.models import FlatPage File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/contrib/flatpages/models.py", line 1, in from django.contrib.sites.models import Site File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/contrib/sites/models.py", line 78, in class Site(models.Model): File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/db/models/base.py", line 108, in new app_config = apps.get_containing_app_config(module) File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "/home/nick/.virtualenvs/psweb/lib/python3.8/site-packages/django/apps/registry.py", line 135, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/280#issuecomment-734486758, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEKCFKLPKDXZSQRI7YIL3SR3DRRANCNFSM4T6EXO7A .

nicorellius commented 3 years ago

As I was writing up a response to you about the different things I tried, I realized something important about the error when Django tries to import the FlatPage model:

from django.contrib.flatpages.models import FlatPage

Django doesn't even get to the app config or the watson.register call. And based on your docs and your insistence that it should work (which I was hoping would be true), I then tried this:

apps.py

from django.apps import AppConfig

class SearchConfig(AppConfig):
    name = 'search'

    def ready(self):
        from django.contrib.flatpages.models import FlatPage
        from watson import search as watson
        watson.register(FlatPage)

This only imports the FlatPage model when the ready method is called, which bypasses the original error condition and registers the app correctly.

Should I keep from watson import search as watson in ready or move it to the top, with the other import statement?

etianen commented 3 years ago

Your code snippet is correct! Keep it like that!

On Fri, 27 Nov 2020 at 15:41, Nick Vincent-Maloney notifications@github.com wrote:

As I was writing up a response to you about the different things I tried, I realized something important about the error when Django tries to import the FlatPage model:

from django.contrib.flatpages.models import FlatPage

Django doesn't even get to the app config or the watson.register call. And based on your docs and your insistence that it should work (which I was hoping would be true), I then tried this:

apps.py

from django.apps import AppConfig

class SearchConfig(AppConfig): name = 'search'

def ready(self):
    from django.contrib.flatpages.models import FlatPage
    from watson import search as watson
    watson.register(FlatPage)

This only imports the FlatPage model when the ready method is called, which bypasses the original error condition and registers the app correctly.

Should I keep from watson import search as watson in ready or move it to the top, with the other import statement?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/280#issuecomment-734890432, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEKCB5JGDONHFEWOIO2MDSR7CCBANCNFSM4T6EXO7A .

nicorellius commented 3 years ago

Thanks.