0

I'm new to Django. I'm trying to add another database to my project. I got this error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Based on this, I added this code to my settings:

import django
django.setup()

I got the same error again.

A few solutions pointed to django.contrib.sites and django.contrib.contenttypes. So I made sure they're in settings.py:

INSTALLED_APPS = [
    'theme',
    'myapp',
    'debug_toolbar',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
]

SITE_ID = 1

A couple of solutions said to remove the apps one by one to see what's wrong, so I removed theme, myapp, and debug_toolbar one by one, and then all at the same time. But I still got the error.

I point to a database router in settings.py too:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'movies': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'movie_data.sql'),
    }
}

from . import router # I shifted this to the top of the file later.
DATABASE_ROUTERS = [router.MoviesRouter]

This solution makes sense so I tried putting DATABASE_ROUTERS = [router.MoviesRouter] all the way down at the end of settings.py, but it didn't help.

urls.py in the same directory as settings.py:

from django.contrib import admin
from django.urls import path
from django.conf import settings

from myapp import views as myapp_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('myapp/', myapp_views.MoviesListView.as_view(), name='movie_list'),
]

In router.py, in the same directory as settings.py:

from myapp import models

class MoviesRouter(object):

    def db_for_read(self, model, **hints):

        model = models.Movies

        if model._meta.app_label == 'myapp':
            return 'movie_data.sql'

        return None

I added model = models.Movies to the code above because there was an error: TypeError at /myapp/ and db_for_read() missing 1 required positional argument: 'model'. But none of code I read on GitHub has anything like it. So I don't know if I'm doing the right thing even if it made the error go away.

models.py in the app:

from django.db import models

class Movies(models.Model):
    id = models.IntegerField(primary_key=True)
    adult = models.TextField()
    original_language = models.TextField()
    original_title = models.TextField()
    title = models.TextField()
    overview = models.TextField()
    release_date = models.DateTimeField()
    genres = models.TextField()
    production_countries = models.TextField()
    videos = models.TextField()
    images = models.TextField()

    class Meta:
        app_label = "myapp"
        db_table = "movies"

(I'm not sure yet if I should add all these fields myself. The database comes with all of them already.)

views.py in the app (where I am trying to practise using generic.Listview for the first time):

from django.views import generic
from .models import Movies

class MoviesListView(generic.ListView):
    model = Movies
    template_name = r'myproject/theme/templates/myapp/myapp.html'

The code started out the same as in the documentation on multiple databases. But reading through other articles, tutorials, and code on GitHub has changed it here and there. So, there may be several mistakes in the code. I don't know for sure which mistakes affect the problem, but I think my primary problem is the one in my first paragraph: Apps aren't loaded yet. What have I missed?

nusantara
  • 1,109
  • 1
  • 15
  • 38

1 Answers1

0

I asked on the Django IRC channel and the solution turns out to be:

  1. Remove import django and django.setup() from settings.py.

  2. Instead of return 'movie_data.sql', return the table in the database instead: return 'movies'.

  3. The DATABASE_ROUTERS list should contain strings. So instead of [router.MoviesRouter], use ['router.MoviesRouter'].

Thank you, Django IRC channel.

nusantara
  • 1,109
  • 1
  • 15
  • 38