Django迁移数据库django.db.utils.ProgrammingError:关系"django_site"不存在 [英] Django Migrating DB django.db.utils.ProgrammingError: relation "django_site" does not exist

查看:39
本文介绍了Django迁移数据库django.db.utils.ProgrammingError:关系"django_site"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为Django做站点升级,现在当我尝试 python manage.py makemigrations 时将其推送到服务器

Doing a site upgrade for Django, now pushing it to the server when I try python manage.py makemigrations I get this error

(kpsga) sammy@kpsga:~/webapps/kpsga$ python manage.py makemigrations
Traceback (most recent call last):
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedTable: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...
                                                            ^


The above exception was the direct cause of the following exception:
...

File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/sammy/webapps/kpsga/kpsga/urls.py", line 27, in <module>
    path('blog', include('blog.urls')),
...
File "/home/sammy/webapps/kpsga/blog/urls.py", line 2, in <module>
    from blog.views import LatestBlogEntries, blog_archive, blog_entry_by_id, blog_entry
File "/home/sammy/webapps/kpsga/blog/views.py", line 10, in <module>
    class LatestBlogEntries(Feed):
File "/home/sammy/webapps/kpsga/blog/views.py", line 11, in LatestBlogEntries
    current_site = Site.objects.get_current()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/contrib/sites/models.py", line 58, in get_current
    return self._get_site_by_id(site_id)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/contrib/sites/models.py", line 30, in _get_site_by_id
    site = self.get(pk=site_id)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 425, in get
    num = len(clone)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
    cursor.execute(sql, params)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "django_site" does not exist
LINE 1: ..."django_site"."domain", "django_site"."name" FROM "django_si...

尽管我已将它们添加到设置文件中

though I've got these added to the settings file

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',


   
    #additional django
    'django.contrib.sites',
]

SITE_ID = 1

LatestClass

LatestClass

class LatestBlogEntries(Feed):
    current_site = Site.objects.get_current()
    title = current_site.name + " - Latest News"
    link = "/news/"
    description = "Latest news and updates from " + current_site.domain
    
    def items(self):
        return BlogEntry.objects.all()[:10]
    def item_title(self, item):
        return item.title
    def item_description(self, item):
        return item.description

推荐答案

您不应在 LatestBlogEntries 类中调用 Site.objects.get_current().它需要数据库查找,当您尚未运行初始迁移时,这会导致关系不存在错误.

You shouldn't call Site.objects.get_current() in the LatestBlogEntries class. It requires a database lookup, which causes the relation does not exist error when you haven't run the initial migrations yet.

您可以使用 title description 的方法.这样, Site.objects.get_current()在访问提要时运行,而不是在Django启动时运行.

You can use methods for title and description. This way, the Site.objects.get_current() runs when the feed is accessed, not when Django starts.

class LatestBlogEntries(Feed):

    def title(self, obj):
        current_site = Site.objects.get_current()
        return current_site.name + " - Latest News"

    link = "/news/"

    def description(self, obj):
        current_site = Site.objects.get_current()
        return "Latest news and updates from " + current_site.domain
    
    def items(self):
        return BlogEntry.objects.all()[:10]
    def item_title(self, item):
        return item.title
    def item_description(self, item):
        return item.description

这篇关于Django迁移数据库django.db.utils.ProgrammingError:关系"django_site"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆