【问题标题】:ValueError in Django when running "python manage.py test"运行“python manage.py test”时Django中的ValueError
【发布时间】:2016-06-25 14:31:18
【问题描述】:

我正在浏览这个网站上的 Django 教程:https://docs.djangoproject.com/en/1.9/intro/tutorial05/

当我尝试运行“python manage.py test”时,我收到以下错误消息:

我正在使用 python3.5 和 PyCharm 运行 Django。有谁知道导致问题的原因以及如何解决?

我的 apps.py 看起来像这样:

from django.apps import AppConfig

class PollsConfig(AppConfig):
    name = 'polls'

我的模型看起来像这样:

from django.db import models
from django.utils import timezone
import datetime

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

这是错误信息:

    Creating test database for alias 'default'...
Traceback (most recent call last):
  File "E:/Dropbox/WebPage/DjangoTestFolder/DjangoTest/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\commands\test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\commands\test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\commands\test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\test\runner.py", line 532, in run_tests
    old_config = self.setup_databases()
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\test\runner.py", line 482, in setup_databases
    self.parallel, **kwargs
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\test\runner.py", line 726, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\backends\base\creation.py", line 70, in create_test_db
    run_syncdb=True,
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\__init__.py", line 119, in call_command
    return command.execute(*args, **defaults)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\migration.py", line 115, in apply
    operation.state_forwards(self.app_label, project_state)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\operations\fields.py", line 51, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\state.py", line 148, in reload_model
    self.apps.render_multiple(states_to_be_rendered)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\state.py", line 296, in render_multiple
    model.render(self)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\migrations\state.py", line 585, in render
    body,
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\base.py", line 158, in __new__
    new_class.add_to_class(obj_name, obj)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\base.py", line 307, in add_to_class
    value.contribute_to_class(cls, name)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\fields\related.py", line 706, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\fields\related.py", line 306, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\fields\related.py", line 86, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\fields\related.py", line 84, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "E:\Dropbox\Python\DjangoEnv\lib\site-packages\django\db\models\utils.py", line 13, in make_model_tuple
    app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

【问题讨论】:

  • 请使用 models.py 和 apps.py 更新您的问题
  • @RasmusRavnFrost 请edit 包含代码而不是链接到外部源的问题。
  • 好的,Ravi,我已经添加了您要求的文件。此外,我在 bitbucket 上添加了一个公共分支,如果需要,应该在此链接上完整显示项目:bitbucket.org/RasmusRavnFrost/djangotestingforstackoverflow/src/…

标签: python django python-3.5


【解决方案1】:

polls/migrations/0001_initial.py line no 36to='polls.models.QUESTION' 有错误

field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.models.QUESTION'),

应该是to='polls.Question'

field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='polls.Question'),

【讨论】:

  • 谢谢。现在就像一个魅力:) 知道错误是如何出现的吗?因为我从未在 polls/migrations/0001_initial.py 文件中进行任何手动更改。
  • @RasmusRavnFrost 我无法用您的代码重现该错误,想知道为什么会发生此错误!
  • 好吧,我想这是个谜。在使用了更多之后,我最好的猜测是出现了错误,因为我在 models.py 中进行了一些更改,但没有正确迁移它们。我可能在进行迁移时让“manage.py runserver polls”一直在运行,这可能是个问题。但这都是纯粹的猜测。无论如何,谢谢。
猜你喜欢
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
相关资源
最近更新 更多