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

时间:2022-09-08 20:25:18

I am reorganizing one of my projects to be more re-usable and just generally structured better and am now getting the error below whenever I run makemigrations - I've spent half the day trying to figure this out on my own but have run out of Google results on searches and am in need of some assistance. What I've done was remove a custom user model I had setup so I can use Django's built-in User model and I also namespaced my apps urls. I don't want to include a bunch of code yet that will do nothing but dirty up this post as I am hoping the Traceback has clues that I am not seeing. If you are looking at this and have an idea of what could be the culprit for the error, can you please advice on what you need to see to offer assistance? Thank you.

我正在重新组织我的一个项目,以便更加可重复使用,并且通常结构更好,每当我运行makemigrations时我现在都会收到错误 - 我花了半天的时间试图自己解决这个问题但是已经用完了谷歌搜索结果,需要一些帮助。我所做的是删除我设置的自定义用户模型,这样我就可以使用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)

4 个解决方案

#1


6  

This error would only occur if split() returns more than 2 elements:

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

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

This means that either app_label or model_name has a dot (.) in it. My money is on the former as model names are automatically generated

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

#2


8  

This issue also occurs if you use the refactor tool in Pycharm and accidentally rename a model's name for the entire project instead of for a single file. This effects the migration files as well, and as a result, the makemigrations command doesn't know what to do and throws the Value error.

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

I fixed it by going into all of the migration files and renaming these lines:

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

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

to:

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

#3


2  

This also happens when you refer another model in your model definition from another app in incorrect way.

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

Check this bug report - https://code.djangoproject.com/ticket/24547

检查此错误报告 - 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').

path应该是'myapp.MyModel'形式,不应该包含包含模型的模块的名称(通常是'models')。

The bug is in the state worksforme, and mostly will not be taken up for fixing.

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

#4


1  

choices expected 2 arguments. if it is more or less then 2 you get this error.

选择预期2个论点。如果它大于或小于2则会出现此错误。

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

#1


6  

This error would only occur if split() returns more than 2 elements:

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

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

This means that either app_label or model_name has a dot (.) in it. My money is on the former as model names are automatically generated

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

#2


8  

This issue also occurs if you use the refactor tool in Pycharm and accidentally rename a model's name for the entire project instead of for a single file. This effects the migration files as well, and as a result, the makemigrations command doesn't know what to do and throws the Value error.

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

I fixed it by going into all of the migration files and renaming these lines:

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

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

to:

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

#3


2  

This also happens when you refer another model in your model definition from another app in incorrect way.

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

Check this bug report - https://code.djangoproject.com/ticket/24547

检查此错误报告 - 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').

path应该是'myapp.MyModel'形式,不应该包含包含模型的模块的名称(通常是'models')。

The bug is in the state worksforme, and mostly will not be taken up for fixing.

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

#4


1  

choices expected 2 arguments. if it is more or less then 2 you get this error.

选择预期2个论点。如果它大于或小于2则会出现此错误。

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