ValueError:在 Django 中解压的值太多(预期为 2)

Posted

技术标签:

【中文标题】ValueError:在 Django 中解压的值太多(预期为 2)【英文标题】:ValueError: too many values to unpack (expected 2) in Django 【发布时间】:2016-09-11 16:55:29 【问题描述】:

我正在重新组织我的一个项目,使其更易于重用,并且通常结构更好,现在每当我运行 makemigrations 时都会收到以下错误 - 我花了半天时间试图自己解决这个问题但已用完 Google 搜索结果,需要一些帮助。我所做的是删除我设置的自定义用户模型,以便我可以使用 Django 的内置用户模型,并且我还命名了我的应用程序 URL。我不想包含一堆代码,它们只会弄脏这篇文章,因为我希望 Traceback 有我没有看到的线索。如果您正在查看此内容并且知道可能是错误的罪魁祸首,您能否就提供帮助所需的内容提供建议?谢谢。

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/core/management/commands/makemigrations.py", line 132, in handle
    migration_name=self.migration_name,
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/autodetector.py", line 45, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/autodetector.py", line 128, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/state.py", line 166, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/state.py", line 228, in __init__
    self.render_multiple(list(models.values()) + self.real_models)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/state.py", line 296, in render_multiple
    model.render(self)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/migrations/state.py", line 585, in render
    body,
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/base.py", line 158, in __new__
    new_class.add_to_class(obj_name, obj)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/fields/related.py", line 707, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/fields/related.py", line 307, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/fields/related.py", line 84, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/site-packages/django/db/models/fields/related.py", line 82, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "/Users/rooster/.virtualenvs/ddm_dev/lib/python3.5/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】:

仅当split() 返回超过 2 个元素时才会出现此错误:

app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)

这意味着app_labelmodel_name 中有一个点(.)。我的钱是前者,因为型号名称是自动生成的

【讨论】:

只是快速跟进您的回答。它确实是一个 (.) 但我之前找不到它们的原因是因为当我在重命名我的应用程序后在路径中进行替换时,它改变了所有迁移并在应用程序名称中添加了 (.) .再次感谢你。我肯定学到了一些新东西。【参考方案2】:

如果您使用 Pycharm 中的重构工具并意外地为整个项目而不是单个文件重命名模型的名称,也会出现此问题。这也会影响迁移文件,因此 makemigrations 命令不知道该做什么并引发 Value 错误。

我通过进入所有迁移文件并重命名这些行来修复它:

field=models.ForeignKey(default=1, null=True, on_delete=django.db.models.deletion.CASCADE, to='books.models.Topic'),

到:

field=models.ForeignKey(default=1, null=True, on_delete=django.db.models.deletion.CASCADE, to='books.Topic'),

【讨论】:

这刚刚发生,手动从 VCS 更改中恢复非常烦人。 是的,看起来像一个 pycharm 问题。 Pycharm 更改“to”选项并阻止迁移。【参考方案3】:

当您以不正确的方式从另一个应用程序引用模型定义中的另一个模型时,也会发生这种情况。

查看此错误报告 - https://code.djangoproject.com/ticket/24547

path should be of the form 'myapp.MyModel' and should NOT include the name of module containing models (which is usually 'models').

该错误处于状态工作表中,并且大部分不会被用于修复。

【讨论】:

【参考方案4】:

选择预期的 2 个参数。如果大于或小于 2,则会出现此错误。

 all_choices = (('pick1', 'value1' ), ('pick2', 'value2'), ('pick3', 'value3'))

【讨论】:

以上是关于ValueError:在 Django 中解压的值太多(预期为 2)的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:python2.7 的 zip 解压的值太多

异常类型:ValueError 异常值:解包的值太多 - Django

获取 ValueError:在尝试将匹配的文件名从 csv 复制到另一个目录时,没有足够的值来解压(预期为 2,得到 1)

syncdb - ValueError:解包的值太多

为啥这个 Django 错误“要解压的值太多”?

UUIDField在Django Model中的使用经验