Adding database to Mezzanine project produces "sqlite3.OperationalError: no such table: django_site"

Adding database to Mezzanine project produces "sqlite3.OperationalError: no such table: django_site"

我正在尝试将第三方数据库添加到 Mezzanine 4.2.3 项目(根据 requirements.txt,它使用 Django 1.10.8)。它由名为 food_crud 的应用程序使用。但是,它会产生此错误:sqlite3.OperationalError: no such table: django_site.

我读过的大多数解决方案都针对 makemigrationsmigrate。少数人说要将 django.contrib.sites 添加到 INSTALLED_APPS。我都试过了。

项目目录布局:

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

food_crud 应用程序位于 INSTALLED_APPSsettings.py 还包含数据库详细信息:

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']

这是 router.py,与 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'),
...

回溯:

  File "C:\Users\alex\Desktop\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 379, in get
num = len(clone)
  File "C:\Users\alex\Desktop\Python0214~1\food\env_food\lib\site-packages\django\db\models\query.py", line 238, in __len__
self._fetch_all()
  File "C:\Users\alex\Desktop\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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\Python0214~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

同样的项目目录布局及其各种设置(包括数据库设置)与 Django 2.0 项目配合良好。我在这个夹层项目中复制了它们,但编辑了名称以适合它。我错过了什么?

您的路由器将模型硬编码为电影,这并不奇怪意味着它始终与 food_crud 应用匹配。所以所有数据库请求都被路由到您的 sqlite 数据库。

不要那样做;删除 model 变量的覆盖。