django-shop
django-shop copied to clipboard
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
Hi
I import ShopPluginBase from shop.cascade.plugin_base in my project, but failed cause "django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet."
I used the django-shop example as below: myshop/cascade.py
from cms.plugin_pool import plugin_pool
from shop.cascade.plugin_base import ShopPluginBase
class MySnippetPlugin(ShopPluginBase):
name = "My Snippet"
render_template = 'myshop/cascade/my-snippet.html'
plugin_pool.register_plugin(MySnippetPlugin)
settings.py
CMSPLUGIN_CASCADE_PLUGINS = (
'cmsplugin_cascade.segmentation',
'cmsplugin_cascade.generic',
'cmsplugin_cascade.link',
'shop.cascade',
'cmsplugin_cascade.bootstrap3',
'myshop.cascade',
...
)
myshop/cascade/my-snippet.html
{% load static sekizai_tags %}
{% addtoblock "css" %}<link href="{% static 'myshop/css/my-snippet.css' %}" rel=
˓→"stylesheet" type="text/css" />{% endaddtoblock %}
{% addtoblock "js" %}<script src="{% static 'myshop/js/my-snippet.js' %}" type="text/
˓→javascript"></script>{% endaddtoblock %}
<div>
my snippet code goes here...
</div>
Thanks in advance
The error you're encountering ("Models aren't loaded yet") typically occurs when you're trying to access Django models before they have been fully loaded. In your provided example, the issue might be due to the order in which the Django app is being loaded.
To address this issue, you can make use of the AppConfig.ready()
method to ensure that your code is executed after the models have been loaded. Here's how you can modify your code to avoid this error:
- In your
myshop
app, create anapps.py
file (if it doesn't already exist):
# myshop/apps.py
from django.apps import AppConfig
class MyShopConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'myshop'
def ready(self):
# Import and register your plugin here
from cms.plugin_pool import plugin_pool
from shop.cascade.plugin_base import ShopPluginBase
class MySnippetPlugin(ShopPluginBase):
name = "My Snippet"
render_template = 'myshop/cascade/my-snippet.html'
plugin_pool.register_plugin(MySnippetPlugin)
- In your
settings.py
, point to your customAppConfig
:
# settings.py
# ...
INSTALLED_APPS = [
# ...
'myshop.apps.MyShopConfig', # Point to your AppConfig
# ...
]
# ...
By using the AppConfig.ready()
method, you can ensure that your plugin registration code is executed at the appropriate time, after the models have been loaded. This should help you avoid the "Models aren't loaded yet" error.
Remember to replace 'myshop'
with the actual name of your Django app, and adjust the paths accordingly based on your project's structure.