Search code examples
djangodjango-modelsdjango-admin

Django ValueError, not enough values to unpack (expacted 4, got 2)


I want to change my database from sqlite3 to mysql, but every time I do so, I encounter an error whenever I try to retrieve data from the database:

enter image description here

Even when I try to log in to admin panel, I get the same error! (createsuperuser works fine): enter image description here

Here are my models:

class Website(models.Model):
    name = models.CharField(max_length=500)
    URL = models.CharField(max_length=500)
    Logo = models.CharField(max_length=1000,null=True,blank=True)

class Data(models.Model):
    website = models.ForeignKey(Website, on_delete=models.CASCADE , null=True, blank=True)
    Target_website= models.CharField(max_length=500,null=True,blank=True)
    URL = models.CharField(max_length=500,null=True,blank=True)
    Title = models.CharField(max_length=1000, unique=True)
    Description = models.TextField(null=True,blank=True)
    Keywords = models.TextField( null=True,blank=True)
    Text = models.TextField(    null=True,blank=True)
    Links = models.TextField(  null=True,blank=True)
    Images = models.TextField(  null=True,blank=True)
    Videos = models.TextField(  null=True,blank=True)
    Claps = models.IntegerField(default=0)
    TimeStamp = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.Title

    def change_claps(self, x):
        self.Claps += x
        self.save()



class CustomUser(AbstractUser):
    username = None
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=14)
    last_name = models.CharField(max_length=14)
    password = models.CharField(max_length=20)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(auto_now_add=True) #auto_now = True if you want to add a field like "updated_on"
    favourite_posts = models.ManyToManyField(Data, blank=True, null=True)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    objects = UserManager()


class Claps(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='user_likes')
    data = models.ForeignKey(Data, on_delete=models.CASCADE, related_name='post_likes')


class Fav(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='user_favs')
    data = models.ForeignKey(Data, on_delete=models.CASCADE, related_name='post_favs')

Here are my serializers:

from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import Data, CustomUser

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = CustomUser
        # fields = ('id', 'username', 'email', 'password')
        # extra_kwargs = {'password': {'write_only': True}
        fields = "__all__"


class DataSerializer(serializers.ModelSerializer):
    class Meta:
        model = Data
        fields = "__all__"

Here is my manager.py:

from django.contrib.auth.base_user import BaseUserManager


class UserManager(BaseUserManager):
    use_in_migrations = True

    def create_user(self, email, password=None, **extra_fields):
        print("creating user")
        if not email:
            raise ValueError("Email not provided!")
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_active', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('SuperUser must have is_staff true!')

        return self.create_user(email, password, **extra_fields)

Maybe it has to do something with the models.ManyToManyfield. I have a client that is impatiently waiting for the web app. Please take some time to solve my problem!


Solution

  • Thank you Zemogle but I found the solution.

    backup your data, (just in case) and just increase the max_length of the password field to something like 200. Note that all operations related to the users who were created with the low password length will give the same error.

    class CustomUser(AbstractUser):
        ....
        password = models.CharField(max_length=200)
        ....
    

    This solution is no where on the WHOLE INTERNET so please spread this simple but effective solution everywhere you can. It took me days of grinding to find the problem.