运行“python manage.py test”时,Django中的ValueError

时间:2016-06-25 14:31:19

标签: python django python-3.5

我正在浏览这个网站的Django tuturial: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)

1 个答案:

答案 0 :(得分:1)

polls/migrations/0001_initial.py line no 36 to='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'),