ValueError: too many values to unpack (expected 2) in Django

Question:

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.

    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)
Asked By: Charles Smith

||

Answers:

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

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

Answered By: Selcuk

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

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

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

Answered By: iankit

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.

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'),
Answered By: Artur

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

 all_choices = (('pick1', 'value1' ), ('pick2', 'value2'), ('pick3', 'value3'))
Answered By: Nils Zenker

You haven’t provided the tuple for us to debug. However, it’s worth knowing that tuples containing single item require a trailing comma.

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

Its a common mistake and it leads to the error

ValueError: too many values to unpack (expected 2)