【问题标题】:Djago error with PostgreSQL migrationPostgreSQL 迁移的 Djago 错误
【发布时间】:2017-05-15 20:34:00
【问题描述】:

我试图用 PostgreSQL 运行我的 Django 应用程序,因为我之前使用 sqlite 的数据库引擎, 这是我的settings.py。我是按照教程https://djangogirls.gitbooks.io/django-girls-tutorial-extensions/optional_postgresql_installation/进行安装的

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'djangogirls',
        'USER': 'cherzik',
        'PASSWORD': 'ADMIN',
        'HOST': 'localhost',
    }
}

这是错误,请帮助

d:\py\djangogirls>python manage.py migrate
System check identified some issues:

WARNINGS:
blog.Comment.created_date: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default f
or this field. This may not be what you want. If you want to have the current da
te as default, use `django.utils.timezone.now`
blog.Post.created_date: (fields.W161) Fixed default value provided.
        HINT: It seems you set a fixed date / time / datetime value as default f
or this field. This may not be what you want. If you want to have the current da
te as default, use `django.utils.timezone.now`
Traceback (most recent call last):
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 213, in ensure_connection
    self.connect()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\postgresql\base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\psycopg2\__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\core\management\commands\migrate.py", line 83, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\migrations\executor.py", line 20, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\migrations\loader.py", line 52, in __init__
    self.build_graph()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\migrations\loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\migrations\recorder.py", line 65, in applied_migrations
    self.ensure_schema()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\migrations\recorder.py", line 52, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_name
s(self.connection.cursor()):
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 254, in cursor
    return self._cursor()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 229, in _cursor
    self.ensure_connection()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 213, in ensure_connection
    self.connect()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 213, in ensure_connection
    self.connect()
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\base\base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\django\db\backends\postgresql\base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Кирюшик\AppData\Local\Programs\Python\Python35\lib\site-package
s\psycopg2\__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError

【问题讨论】:

  • 请发布您的模型。
  • 您的代码似乎无法连接到 PostgreSQL。检查您是否已安装并运行 PostgreSQL,并且在 Django 设置中正确设置了 postgresql 数据库的名称、用户和密码。

标签: django postgresql migration


【解决方案1】:

这是我的模型,如果我将 created_date 替换为 timezone.now 就像你说的那样有错误

from django.db import models
from django.utils import timezone
# Create your models here.

class Post(models.Model):
    author = models.ForeignKey('auth.User')
    title = models.CharField(max_length = 200)
    text = models.TextField()
    created_date = models.DateTimeField(default=timezone.now())
    published_date = models.DateTimeField(blank = True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()


    def __str__(self):
        return self.title

    def approved_comments(self):
        return self.comments.filter(approved_comment = True)

class Comment(models.Model):
    post = models.ForeignKey('blog.Post', related_name='comments')
    author = models.CharField(max_length=200)
    text = models.TextField()
    created_date = timezone.now()
    approved_comment = models.BooleanField(default=False)

    def approve(self):
        self.approved_comment = True
        self.save()

    def __str__(self):
        return self.text

【讨论】:

    【解决方案2】:

    像这样更改 created_date 字段,

    created_date = models.DateField(default=timezone.now)
    

    或者

    created_date = models.DateField(default=datetime.date.today)
    

    去掉函数的括号。 使用括号,该函数只会在 models.py 加载时被调用。 如果我们删除括号,我们会将一个可调用对象传递给模型,并且每次保存新实例时都会调用它。

    【讨论】:

      【解决方案3】:

      在评论和帖子模型中更改您的 created_date 字段。

      created_date = models.DateField(default=timezone.now)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-23
        • 1970-01-01
        • 1970-01-01
        • 2018-07-06
        • 1970-01-01
        相关资源
        最近更新 更多