ValueError:Django中要解包的值太多(应为2个)

6rqinv9w  于 2022-11-26  发布在  Go
关注(0)|答案(5)|浏览(188)

我正在重新组织我的一个项目,使其更可重用,结构更好,现在每当我运行makemigrations时都会出现下面的错误--我花了半天时间试图自己解决这个问题,但Google搜索结果已经用完了,我需要一些帮助。我所做的是删除我设置的自定义用户模型,这样我就可以使用Django的内置-在用户模型中,我还命名了我的应用程序url。我不想包括一堆代码,但会做什么,但弄脏了这篇文章,因为我希望跟踪有线索,我没有看到。如果你正在看这个,并有一个想法,什么可能是错误的罪魁祸首,你能请建议你需要看到什么来提供帮助?谢谢。

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)
dffbzjpn

dffbzjpn1#

仅当split()返回2个以上的元素时,才会发生此错误:

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

这意味着app_labelmodel_name中有一个点(.)。

4c8rllxm

4c8rllxm2#

如果您在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'),
pw9qyyiw

pw9qyyiw3#

当您以不正确的方式从另一个应用程序引用模型定义中的另一个模型时,也会发生这种情况。
检查此错误报告-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').
这个bug处于worksform状态,大部分不会被修复。

gzjq41n4

gzjq41n44#

choices需要2个参数。如果大于或小于2,则会出现此错误。

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

jv2fixgn5#

您尚未提供元组供我们调试。但是,值得注意的是,包含单个项的元组需要一个尾随逗号。

all_choices = (('pick1', 'value1' ),)

这是一个常见的错误,它导致了错误

ValueError: too many values to unpack (expected 2)

相关问题