[Fixed]-ValueError: too many values to unpack (expected 2) in Django

10👍

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

👤Selcuk

9👍

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'),
👤Artur

4👍

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.

👤iankit

4👍

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

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

0👍

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)

Leave a comment