I'm trying to add a third-party database to a Mezzanine 4.2.3 project (it uses Django 1.10.8 according to requirements.txt
). It's to be used by an app called food_crud
. However, it produces this error: sqlite3.OperationalError: no such table: django_site
.
Most solutions I've read say to makemigrations
and migrate
. A few say to add django.contrib.sites
to INSTALLED_APPS
. I've tried both.
Project directory layout:
180214 food
- env_food # virtualenv
- food
__init__.py
settings.py
router.py
urls.py
views.py
wsgi.py
- food_crud
- migrations
__init__.py
admin.py
models.py
tests.py
views.py
- static
...
- theme
...
__init__.py
manage.py
movie_data.sql
The food_crud
app is in INSTALLED_APPS
. settings.py
also contains the database details:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "db_nate",
"USER": "alex",
"PASSWORD": "oh no you did not use password did you",
"HOST": "localhost",
"PORT": "",
},
"movies": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "movie_data.sql",
}
}
DATABASE_ROUTERS = ['food.router.FoodRouter']
Here's router.py
, in the same folder as settings.py
:
from food_crud import models
class FoodRouter(object):
def db_for_read(self, model, **hints):
model = models.Movies
if model._meta.app_label == 'food_crud':
return 'movies'
return None
models.py
:
from django.db import models
class Movies(models.Model):
id = models.AutoField(primary_key=True)
adult = models.TextField()
original_language = models.TextField()
original_title = models.TextField()
title = models.TextField()
overview = models.TextField()
release_date = models.DateField()
genres = models.TextField()
production_countries = models.TextField()
videos = models.TextField()
images = models.TextField()
class Meta:
app_label = "food_crud"
db_table = "movies"
managed = False
views.py
:
from django.views import generic
from .models import Movies
class MoviesListView(generic.ListView):
model = Movies
template_name = r'"/food/theme/templates/food_crud/food_crud.html"'
urls.py
:
...
from . import views
from food_crud import views as food_crud_views
urlpatterns += [
url("^$", views.blog_post_list_index, name="home"),
url('food/', food_crud_views.MoviesListView.as_view(), name='food_list'),
...
Traceback:
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\core\handlers\base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\core\handlers\base.py", line 178, in _get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\pages\middleware.py", line 75, in process_view
for_user=request.user, include_login_required=True)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\pages\managers.py", line 68, in with_ascendants_for_slug
pages_for_user = self.published(**kwargs)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\pages\managers.py", line 24, in published
published = super(PageManager, self).published(for_user=for_user)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\core\managers.py", line 70, in published
Q(status=CONTENT_STATUS_PUBLISHED))
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\core\managers.py", line 378, in get_queryset
lookup = {self.__field_name + "__id__exact": current_site_id()}
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\mezzanine\utils\sites.py", line 56, in current_site_id
site = Site.objects.get(domain__iexact=domain)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 379, in get
num = len(clone)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 238, in __len__
self._fetch_all()
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 1087, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 54, in __iter__
results = compiler.execute_sql()
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 164, in execute
return self._record(self.cursor.execute, sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\debug_toolbar\panels\sql\tracking.py", line 106, in _record
return method(sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\alex\Desktop\Python\180214~1\food\env_food\lib\site-packages\django\db\backends\sqlite3\base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_site
This same project directory layout and its various settings, including for the databases, work well with a Django 2.0 project. I duplicated them in this Mezzanine project but with names edited to fit it. What have I missed?
Your router hard codes the model to Movies, which not surprisingly means it always matches the food_crud app. So all db requests are being routed to your sqlite db.
Don't do that; remove that overriding of the model
variable.