ValueError: Field 'id' expected a number but got 'demo@gmail.com'

Viewed 100

I am trying to create a User model so users can register through an email address instead of a username. I am getting ValueError: Field 'id' expected a number but got 'demo@gmail.com'. when I try to create superuser their was no issue in makemigrations and migrate

also try to delete pycache and all the old migrations and db file but still same. colsole output is also at the end here are Models

User model

class User(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_("email address"), unique=True)
    first_name = models.CharField(_("first name"), max_length=10, blank=True)
    last_name = models.CharField(_("last name"), max_length=10, blank=True)
    date_joined = models.DateTimeField(_("date joined"), auto_now_add=True)
    is_active = models.BooleanField(_("is active"), default=True)

    # User Manager here
    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
    
    def get_full_name(self):
        full_name = self.first_name + " " + self.last_name
        return full_name.strip()

User Manager

class UserManager(BaseUserManager):
    use_in_migrations = True

    def _create_user(self,email,password, **extra_fields):
        """
        Create and save user
        """
        if not email:
            raise ValueError("Email is not available")
        email = self.normalize_email(email)
        user = self.model(email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user
    
    def create_user(self,email, password=None, **extra_fields):
        """
        Create a user
        """
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)
    
    def create_superuser(self,email, password, **extra_fields):
        """
        Create super user 
        """
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get("is_superuser") is not True:
            raise ValueError("Superuser must have is_superuser=True")

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

colsone

(venv) D:\Python\Django\django-bookshop\bookshop>py manage.py createsuperuser
Email address: demo@gmail.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Traceback (most recent call last):
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1823, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'demo@gmail.com'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Python\Django\django-bookshop\bookshop\manage.py", line 22, in <module>
    main()
  File "D:\Python\Django\django-bookshop\bookshop\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
    return super().execute(*args, **options)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 189, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "D:\Python\Django\django-bookshop\bookshop\account\managers.py", line 35, in create_superuser
    return self._create_user(email, password, **extra_fields)
  File "D:\Python\Django\django-bookshop\bookshop\account\managers.py", line 16, in _create_user
    user.save(using=self._db)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\contrib\auth\base_user.py", line 67, in save
    super().save(*args, **kwargs)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\base.py", line 845, in _save_table
    updated = self._do_update(base_qs, using, pk_val, values, update_fields,
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\base.py", line 879, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\query.py", line 941, in filter
    return self._filter_or_exclude(False, args, kwargs)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\query.py", line 961, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\query.py", line 968, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\sql\query.py", line 1393, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\sql\query.py", line 1412, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\sql\query.py", line 1347, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\sql\query.py", line 1193, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\lookups.py", line 25, in __init__
    self.rhs = self.get_prep_lookup()
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\lookups.py", line 77, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "D:\Python\Django\django-bookshop\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1825, in get_prep_value
    raise e.__class__(
ValueError: Field 'id' expected a number but got 'demo@gmail.com'.
1 Answers

In your _create_user function of the UserManager

user = self.model(email, **extra_fields)

should be

user = self.model(email=email, **extra_fields)

When you initialize a model like shown in first code, the arguments are passed to the fields in the order in which they are defined in the model. First field will be always id. By default, Django gives each model an auto-incrementing primary key.(https://docs.djangoproject.com/en/3.2/topics/db/models/#automatic-primary-key-fields), so here the email value is assigned to the id.

For that reason you need to specify to which field the value email has to be assigned. hence as shown in second code, you need to give email=email

Related