【问题标题】:Django databases: problems with the test databaseDjango 数据库:测试数据库的问题
【发布时间】:2020-12-30 11:38:35
【问题描述】:

我正在学习 Django 测试。 我用一个简单的模型编写了一个简单的 App,并想运行测试来检查模型方法的有效性,但是当我运行测试时收到错误消息:

这里是models.py

from django.db import models


class Trip(models.Model):
    origin = models.CharField(max_length=20)
    destination = models.CharField(max_length=20)

    def __str__(self):
        return self.origin

    def is_valid(self):
        return self.origin != self.destination

这里是 test.py

from django.test import TestCase
from .models import Trip


# Create your tests here.
class TripModelTests(TestCase):

    def test_trip(self):
        a = Trip.objects.create(origin='a', destination='a')
        self.assertIs(a.is_valid(), True)

这里是 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'd57r9kcrhthdc7',
        'USER': 'sdqxaruartlvrd',
        'PASSWORD': 'e7b8f85611596ed125fe3ed4ea590f821f65e317c17ee7871be75b8130d72378',
        'HOST': 'ec2-3-214-46-194.compute-1.amazonaws.com',
        'PORT': '5432',
        'TEST': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    }
}

这是我在运行 python manage.py test transport 时收到的错误消息

Creating test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\management\commands\test.py", line 53, in handle
    failures = test_runner.run_tests(test_labels)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 695, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\runner.py", line 614, in setup_databases
    return _setup_databases(
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\test\utils.py", line 170, in setup_databases
    connection.creation.create_test_db(
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 55, in create_test_db
    self._create_test_db(verbosity, autoclobber, keepdb)
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\base\creation.py", line 172, in _create_test_db
    'dbname': self.connection.ops.quote_name(test_database_name),
  File "C:\Users\fabia\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\postgresql\operations.py", line 113, in quote_name
    if name.startswith('"') and name.endswith('"'):
AttributeError: 'WindowsPath' object has no attribute 'startswith'

如果我只使用默认的 django 设置并使用 sqlite 数据库,则测试工作正常......

【问题讨论】:

    标签: python django


    【解决方案1】:

    错误可能是由于您的BASE_DIR路径,在您的settings.py中,您需要删除斜杠/并将其切换到以下

    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    

    您可能需要根据您的BASE_DIR 内容验证它指向正确的位置。调试此问题的一种方法是在数据库字典之后设置ipdb(),这样一旦使用python manage.py runserver,就可以轻松检查DATABASES 结构。

    import ipdb; ipdb.set_trace()
    

    来源:https://pypi.org/project/ipdb/

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 2014-12-31
      • 2021-07-18
      相关资源
      最近更新 更多