App's ready() method not working

Hi! I’m trying to automatically populate my database with a test user if the environment running is DEBUG=True.
It’s supposed to work with the App’s ready() method but not in my case.

My code:

from django.apps import AppConfig
from django.conf import settings

from app.users.models import User

class UsersConfig(AppConfig):

    name = 'app.users'
    verbose_name = 'Users'

    def ready(self, *args, **kwargs):
        if settings.DEBUG:
            test_email = ''
            test_password = 'easyTest'
            u = User.objects.get(email=test_email)
            if u is None:
                User.objects.create_user(email=test_email, password=test_password)

When I run the server, I’m getting:

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

You can’t import models at the top level of an file. See the documentation for more details and an example. You’ll need to import the models from inside of the ready function.

Let me know if you have questions after looking at the docs!

1 Like