4

I created a custom user and add permission is_driver to check whether my user belongs to Driver's Group.

class CustomUser(AbstractUser):
    mobile = models.CharField(max_length=16)
    address = models.CharField(max_length=100)
    class Meta:
        permissions = (
            ("is_driver", "Can access driver's page")
        )

But when I run manage.py makemigrations and then manage.py migrate, it throws an error: ValueError: too many values to unpack. I'm new to permissions, maybe there is some other way to add permissions to a Group. What's wrong?

Andrew
  • 423
  • 1
  • 6
  • 16

1 Answers1

24

Try to add the missing , at the end of your tuple

class CustomUser(AbstractUser):
    mobile = models.CharField(max_length=16)
    address = models.CharField(max_length=100)
    class Meta:
        permissions = (
            ("is_driver", "Can access driver's page"),
        )

Python is strict about that when a tuple only has one item. To see what happens at Python level you could open the shell and try the following.

>>> foo = (('ham', 'eggs'))
>>> foo
('ham', 'eggs')
>>> foo[0]
'ham'
>>> foo = (('ham', 'eggs'),)
>>> foo
(('ham', 'eggs'),)
>>> foo[0]
('ham', 'eggs')

Long story short, without the , it is a different data structure.

sthzg
  • 5,514
  • 2
  • 29
  • 50