"Apps aren't loaded yet."

I’m trying to execute a test for a model that I recently created, but I’m getting this error.

(base) (micro-videos-3.10) PS C:\Users\kcarw\Documents\micro-videos\src\core\category\tests\integration\infra\django_app> python -m unittest test_int_models.py
Traceback (most recent call last):
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\__main__.py", line 18, in <module>
    main(module=None)
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\main.py", line 100, in __init__
    self.parseArgs(argv)
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\main.py", line 147, in parseArgs
    self.createTests()
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\main.py", line 158, in createTests
    self.test = self.testLoader.loadTestsFromNames(self.testNames,
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\python\cpython@3.10.15\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "C:\Users\kcarw\Documents\micro-videos\src\core\category\tests\integration\infra\django_app\test_int_models.py", line 5, in <module>
    from core.category.infra.django_app.models import CategoryModel
  File "C:\Users\kcarw\Documents\micro-videos\src\core\category\infra\django_app\models.py", line 6, in <module>
    class CategoryModel(models.Model):
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\venvs\micro-videos-2jvG20KW-venv\lib\site-packages\django\db\models\base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\venvs\micro-videos-2jvG20KW-venv\lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "C:\Users\kcarw\AppData\Local\pdm\pdm\venvs\micro-videos-2jvG20KW-venv\lib\site-packages\django\apps\registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

This is my model

from django.db import models
from django.utils import timezone


# Create your models here.
class CategoryModel(models.Model):
    id = models.UUIDField(primary_key=True, editable=True)
    name = models.CharField(max_length=255)
    description = models.TextField(null=True)
    is_active = models.BooleanField(default=True)
    created_at = models.DateField(default=timezone.now())
    
    class Meta:
        db_table = 'categories'

This is my settings

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/


# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'django_extensions',
    "core.category.infra.django_app",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "django_app.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "django_app.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

Welcome @kcarww !

Please post your test class (and any set-up code).

Also, I typically see this error being caused by trying to use the ORM outside of a function or method. Check to verify that you aren’t trying to do a query at the module or class level.

Thank you very much!

This is my test class

import pytest
import unittest
from django.utils import timezone
from core.category.infra.django_app.models import CategoryModel


@pytest.mark.django_db()
class TestCategoryModelInt(unittest.TestCase):

    def test_foo(self):
        arrange = {
            'id' : 'f6e70586-a265-439e-9424-340adda215f9',
            'name' : 'movie',
            'description' : None,
            'is_active' : True,
            'created_at' : timezone.now()
        }
        
        category = CategoryModel.objects.create(**arrange)
        self.assertEqual(category.id, arrange['id'])
        self.assertEqual(category.name, arrange['name'])
        self.assertEqual(category.description, arrange['description'])
        self.assertEqual(category.is_active, arrange['is_active'])
        self.assertEqual(category.created_at, arrange['created_at'])

I also have this configuration class

from django.apps import AppConfig


class CategoryConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "core.category.infra.django_app"