Pytest-django: Model class sites.models.Site doesn't declare an explicit app_label
There have been lots of similar questions, but most of them are irrelevant to pytest
. Running manage.py runserver
works correctly for me, it only fails with pytest.
When running pytest --collectonly --verbose -n0
, pytest
fails with the following error:
============================= test session starts ==============================
platform linux -- Python 3.12.6, pytest-8.3.3, pluggy-1.5.0 -- /home/jasongrace2282/repos/website5/.venv/bin/python3
cachedir: .pytest_cache
django: version: 5.1.3, settings: director.settings (from ini)
rootdir: /home/jasongrace2282/repos/website5/manager
configfile: pyproject.toml
testpaths: director
plugins: django-4.9.0, anyio-4.6.2.post1, xdist-3.6.1
collecting ... collected 1 item / 1 error
<Dir manager>
<Package director>
<Dir apps>
<Package auth>
<Module tests.py>
<Function test_password_login>
Make sure logging in via password on the dev env works.
==================================== ERRORS ====================================
________________ ERROR collecting director/apps/sites/tests.py _________________
director/apps/sites/tests.py:3: in <module>
from . import parser
director/apps/sites/parser.py:6: in <module>
from .models import DockerAction, DockerImage, Domain
director/apps/sites/models.py:39: in <module>
class Site(models.Model):
../.venv/lib/python3.12/site-packages/django/db/models/base.py:134: in __new__
raise RuntimeError(
E RuntimeError: Model class sites.models.Site doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
=========================== short test summary info ============================
ERROR director/apps/sites/tests.py - RuntimeError: Model class sites.models.S...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
====================== 1 test collected, 1 error in 0.17s ======================
My director/apps/sites/apps.py
looks like:
class SitesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "director.apps.sites"
And my settings.INSTALLED_APPS
has the following content:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"debug_toolbar",
"django_browser_reload",
"django_linear_migrations",
"social_django",
"director.apps.auth",
"director.apps.users",
"director.apps.sites",
"heroicons",
]
if DEBUG:
INSTALLED_APPS += [
"django_extensions",
]
Additionally, my directory structure looks like this
.
├── conftest.py
├── director
│ ├── apps
│ │ ├── auth
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ │ └── __init__.py
│ │ │ ├── models.py
│ │ │ ├── oauth.py
│ │ │ ├── tests.py
│ │ │ ├── urls.py
│ │ │ └── views.py
│ │ ├── sites
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ │ ├── 0001_initial.py
│ │ │ │ ├── 0002_alter_dockerimage_name.py
│ │ │ │ ├── __init__.py
│ │ │ │ └── max_migration.txt
│ │ │ ├── models.py
│ │ │ ├── parser.py
│ │ │ ├── tests.py
│ │ │ ├── urls.py
│ │ │ └── views.py
│ ├── asgi.py
│ ├── djtypes.py
│ ├── __init__.py
│ ├── settings
│ │ ├── __init__.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── pyproject.toml
With a pytest config of
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "director.settings"
python_files = ["tests.py", "test_*.py"]