I\'m at wit\'s end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:
Model class django.contrib.co
In my case, this was happening because I used a relative module path in project-level urls.py, INSTALLED_APPS
and apps.py
instead of being rooted in the project root. i.e. absolute module paths throughout, rather than relative modules paths + hacks.
No matter how much I messed with the paths in INSTALLED_APPS
and apps.py
in my app, I couldn't get both runserver
and pytest
to work til all three of those were rooted in the project root.
Folder structure:
|-- manage.py
|-- config
|-- settings.py
|-- urls.py
|-- biz_portal
|-- apps
|-- portal
|-- models.py
|-- urls.py
|-- views.py
|-- apps.py
With the following, I could run manage.py runserver
and gunicorn with wsgi and use portal
app views without trouble, but pytest would error with ModuleNotFoundError: No module named 'apps'
despite DJANGO_SETTINGS_MODULE
being configured correctly.
config/settings.py:
INSTALLED_APPS = [
...
"apps.portal.apps.PortalConfig",
]
biz_portal/apps/portal/apps.py:
class PortalConfig(AppConfig):
name = 'apps.portal'
config/urls.py:
urlpatterns = [
path('', include('apps.portal.urls')),
...
]
Changing the app reference in config/settings.py to biz_portal.apps.portal.apps.PortalConfig
and PortalConfig.name
to biz_portal.apps.portal
allowed pytest to run (I don't have tests for portal
views yet) but runserver
would error with
RuntimeError: Model class apps.portal.models.Business doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
Finally I grepped for apps.portal
to see what's still using a relative path, and found that config/urls.py should also use biz_portal.apps.portal.urls
.