我正在重新组织我的一个项目,以便更加可重复使用,并且通常结构更好,现在每当我运行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)
答案 0 :(得分:8)
如果您在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'),
答案 1 :(得分:7)
仅当split()
返回超过2个元素时才会出现此错误:
app_label, model_name = model.split(".")
ValueError: too many values to unpack (expected 2)
这意味着app_label
或model_name
中有一个点(.
)。我的钱是前者,因为模型名称是自动生成的
答案 2 :(得分: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').
该错误处于状态workforme,并且大多数不会被用于修复。
答案 3 :(得分:1)
选择预期2个参数。如果它大于或等于2则会出现此错误。
all_choices = (('pick1', 'value1' ), ('pick2', 'value2'), ('pick3', 'value3'))