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

I am getting the ValueError when I am trying to create the superuser. here is my users model.py file

class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        if not email:
            raise ValueError('The Email field is required')
        email = self.normalize_email(email)
        user = self.model(email, **extra_fields)
        # user = get_user_model()
        user.set_password(password)
        user.save(using=self._db)
        return user
    def create_superuser(self, email, password=None, **extra_fields):
        extra_fields.setdefault('is_superuser', True)
        extra_fields.setdefault('is_staff', True)
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must be admin')
        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)

class CustomUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_influencer = models.BooleanField(default=False)
    date_joined = models.DateTimeField(auto_now_add=True)

    objects = CustomUserManager()
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['first_name', 'last_name']

    def __str__(self):
        return self.email

I tried to create the superuser from vscode terminal : python manage.py createsuperuser
The traceback error I got is :

Traceback (most recent call last):
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2123, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'jsingh14192@gmail.com'
File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 239, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        **user_data
        ^^^^^^^^^^^
    )
    ^
  File "X:\Programming\Influencer-Engagement\users\models.py", line 24, in create_superuser
    return self.create_user(email, password, **extra_fields)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\users\models.py", line 15, in create_user
    user.save(using=self._db)
    ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\contrib\auth\base_user.py", line 62, in save
    super().save(*args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\base.py", line 891, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2125, in get_prep_value
    raise e.__class__(
        "Field '%s' expected a number but got %r." % (self.name, value),
    ) from e
ValueError: Field 'id' expected a number but got 'jsingh14192@gmail.com'.

I have skipped the middle parts of the error message because it was too big

Hi @jags14 !

When posting code, templates, error messages, or other preformatted text here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.

(Words copied from Ken, to save me typing.)

It will be easier for people to help you that way.

1 Like

Just “ValueError” isn’t enough to help you. You need to post the full exception.

Note: I’ve taken the liberty of editing your post to add the ``` before and after the code - please remember to do this in the future.

Also, in addition to the above (providing the full error message with traceback), please identify how you’re trying to create the user.

1 Like

Hi @philgyford !
Thanks, will keep in mind from next time !

Hi Ken, thanks for getting back.
Thank you for editing it, will make sure that it’s properly edited from the next time.
Here is what I did : after completing the model, i ran migrations which worked just fine.
when i ran the command to createsuperuser, it asked for email, fname, lastname and password… after entering the password for the second time, I am getting this error for the last 2 days…
I have added major portion of the error in the question - somehow, it’s reading email as the ;id’ attribute .
I made sure that in the customuser model, email attribute is not marked as primary_key but still getting the same ValueError that ‘id’ expected a number but got ‘email_id’

here is the full exception :

Traceback (most recent call last):
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2123, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'jsingh14192@gmail.com'

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

Traceback (most recent call last):
  File "X:\Programming\Influencer-Engagement\manage.py", line 22, in <module>
    main()
    ~~~~^^
  File "X:\Programming\Influencer-Engagement\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
    ~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\core\management\base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 90, in execute
    return super().execute(*args, **options)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\core\management\base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 239, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        **user_data
        ^^^^^^^^^^^
    )
    ^
  File "X:\Programming\Influencer-Engagement\users\models.py", line 24, in create_superuser
    return self.create_user(email, password, **extra_fields)
           ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\users\models.py", line 15, in create_user
    user.save(using=self._db)
    ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\contrib\auth\base_user.py", line 62, in save
    super().save(*args, **kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\base.py", line 891, in save
    self.save_base(
    ~~~~~~~~~~~~~~^
        using=using,
        ^^^^^^^^^^^^
    ...<2 lines>...
        update_fields=update_fields,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\base.py", line 997, in save_base
    updated = self._save_table(
        raw,
    ...<4 lines>...
        update_fields,
    )
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\base.py", line 1129, in _save_table
    updated = self._do_update(
        base_qs, using, pk_val, values, update_fields, forced_update
    )
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\base.py", line 1173, in _do_update
    filtered = base_qs.filter(pk=pk_val)
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\query.py", line 1476, in filter
    return self._filter_or_exclude(False, args, kwargs)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\query.py", line 1494, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, args, kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\query.py", line 1501, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\sql\query.py", line 1609, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
                ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\sql\query.py", line 1641, in _add_q
    child_clause, needed_inner = self.build_filter(
                                 ~~~~~~~~~~~~~~~~~^
        child,
        ^^^^^^
    ...<7 lines>...
        update_join_types=update_join_types,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\sql\query.py", line 1555, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\sql\query.py", line 1385, in build_lookup
    lookup = lookup_class(lhs, rhs)
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 30, in __init__
    self.rhs = self.get_prep_lookup()
               ~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 369, in get_prep_lookup
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2125, in get_prep_value
    return super().get_prep_lookup()
           ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\lookups.py", line 88, in get_prep_lookup
    return self.lhs.output_field.get_prep_value(self.rhs)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2125, in get_prep_value
  File "X:\Programming\Influencer-Engagement\env\Lib\site-packages\django\db\models\fields\__init__.py", line 2125, in get_prep_value
    raise e.__class__(
        "Field '%s' expected a number but got %r." % (self.name, value),
    ) from e
ValueError: Field 'id' expected a number but got 'jsingh14192@gmail.com'.

The issue here is that the first field in the User model is the implicit id field in your user model, but your self.model call is trying to pass the email field as the first, positional, parameter to the initializer.

You can either add the email field to the extra_fields dict, or pass it as a keyword parameter.
Either (one or the other, not both):
extra_fields['email'] = self.normalize_email(email)
user = self.model(**extra_fields)
or
user = self.model(email=email, **extra_fields)

1 Like

Got It, and It worked - I will remember this subtle point !!
Thanks a lot :slight_smile: