Makemigrations ValueError:解包的值太多

Posted

技术标签:

【中文标题】Makemigrations ValueError:解包的值太多【英文标题】:Makemigrations ValueError: too many values to unpack 【发布时间】:2016-05-11 08:29:52 【问题描述】:

当我尝试 manage.py makemigrations 时出现错误:

我的模型是:

class Burger(models.Model):
    title = models.CharField(max_length=100)
    mt = models.CharField(max_length=50)
    description = models.TextField(max_length=255)
    price = models.IntegerField()
    img = models.ImageField()

class Comment(models.Model):
    author = models.CharField(max_length=50)
    email = models.CharField(max_length=100)
    text = models.TextField(max_length=255)
    date = models.DateTimeField()
    bid = models.ForeignKey('Burger', on_delete=models.CASCADE)

回溯是:

Traceback (most recent call last):
  File "C:\Burger123\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 327, in execute
    django.setup()
  File "C:\Python27\lib\site-packages\django\__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "C:\Python27\lib\site-packages\django\apps\config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Burger123\BURRGER\models.py", line 15, in <module>
    class Comment(models.Model):
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 158, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 299, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python27\lib\site-packages\django\db\models\fields\related.py", line 703, in contribute_to_class
    super(ForeignObject, self).contribute_to_class(cls, name, virtual_only=virtual_only)
  File "C:\Python27\lib\site-packages\django\db\models\fields\related.py", line 308, in contribute_to_class
    lazy_related_operation(resolve_related_class, cls, self.remote_field.model, field=self)
  File "C:\Python27\lib\site-packages\django\db\models\fields\related.py", line 85, in lazy_related_operation
    return apps.lazy_model_operation(partial(function, **kwargs), *model_keys)
  File "C:\Python27\lib\site-packages\django\db\models\fields\related.py", line 83, in <genexpr>
    model_keys = (make_model_tuple(m) for m in models)
  File "C:\Python27\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

我的INSTALLED_APPS 设置是:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'BURRGER.apps.BurrgerConfig'
]

BurrgerConfig:

from django.apps import AppConfig
class BurrgerConfig(AppConfig):
    name = 'BURRGER'
    label = 'my.burger'

我删除了bid = models.ForeignKey('Burger', on_delete=models.CASCADE) 行,并且makemigrations 没有错误地结束。但我的代码中完全需要这一行,所以问题仍然存在。

【问题讨论】:

不要发布文本图像,而是发布实际文本。以及您尝试/研究的内容。 你可以试试 manage.py makemigration djang_app_name 吗?首次迁移后,您必须输入应用名称。 请不要在编辑问题时删除回溯——它会显示错误是什么以及发生的位置。 再次,到目前为止,您自己尝试/研究了什么。正如您在右侧看到的任何 相关问题中所看到的,此错误返回的原因是您很可能在某处使用了错误的逗号。 (感谢耐心,Alasdair) 问题出在您的AppConfig 班级,请参阅下面的答案。 【参考方案1】:

应用配置中的label 不应包含点。你可以这样做:

class BurrgerConfig(AppConfig):
    name = 'BURRGER'
    label = 'burger'

您的代码更加混乱,因为您在代码和目录名称中拼错了“BURRGER”。 Python/Django 中的常用方法是

burger - directory name, name and lable in app config class
Burger - model name
BurgerConfig - App Config class name

【讨论】:

以上是关于Makemigrations ValueError:解包的值太多的主要内容,如果未能解决你的问题,请参考以下文章

值错误 - Python Django

Django:无法解析相关模型“users.UserProfile”

Django:啥时候运行makemigrations?

避免 makemigrations 重新创建日期字段

Django Migration (makemigrations) 应该与创建模型分开提交吗?

Django Makemigrations 和 Migrate 不断重复