Django invalid literal for int() with base 10: '??????? ???????' when i try to migrate

Question:

I’m trying to create a new tables in a new app added to my project .. makemigrations worked great but migrate is not working .. here is my models

blog/models.py

from django.db import models

# Create your models here.
from fostania_web_app.models import UserModel


class Tag(models.Model):
    tag_name = models.CharField(max_length=250)

    def __str__(self):
        return self.tag_name


class BlogPost(models.Model):
    post_title = models.CharField(max_length=250)
    post_message = models.CharField(max_length=2000)
    post_author = models.ForeignKey(UserModel, on_delete=models.PROTECT)
    post_image = models.ImageField(upload_to='documents/%Y/%m/%d', null=False, blank=False)
    post_tag = models.ForeignKey(Tag, on_delete=models.PROTECT)
    post_created_at = models.DateTimeField(auto_now=True)

when i try to do python manage.py migrate i get this error

invalid literal for int() with base 10: '??????? ???????'

UserModel is created in another app in the same project that is why i used the statement from fostania_web_app.models import UserModel

fostania_web_app/models.py

class UserModelManager(BaseUserManager):
    def create_user(self, email, password, pseudo):
        user = self.model()
        user.name = name
        user.email = self.normalize_email(email=email)
        user.set_password(password)
        user.save()

        return user

    def create_superuser(self, email, password):
        '''
        Used for: python manage.py createsuperuser
        '''
        user = self.model()
        user.name = 'admin-yeah'
        user.email = self.normalize_email(email=email)
        user.set_password(password)

        user.is_staff = True
        user.is_superuser = True
        user.save()

        return user


class UserModel(AbstractBaseUser, PermissionsMixin):
    ## Personnal fields.
    email = models.EmailField(max_length=254, unique=True)
    name = models.CharField(max_length=16)
    ## [...]

    ## Django manage fields.
    date_joined = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELD = ['email', 'name']

    objects = UserModelManager()

    def __str__(self):
        return self.email

    def get_short_name(self):
        return self.name[:2].upper()

    def get_full_name(self):
        return self.name

and on my setting.py files i have this :

#custom_user
AUTH_USER_MODEL='fostania_web_app.UserModel'

and here is the full traeback :

Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, fostania_web
ons, sites, social_django
Running migrations:
  Applying blog.0001_initial...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangocoremanagement__init__.py", line 371, in execute_from_comm
    utility.execute()
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangocoremanagement__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangocoremanagementbase.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangocoremanagementbase.py", line 335, in execute
    output = self.handle(*args, **options)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangocoremanagementcommandsmigrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmigrationsexecutor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=f
nitial=fake_initial)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwar
    state = self.apply_migration(state, migration, fake=fake, fake_in
initial)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmigrationsexecutor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmigrationsmigration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_st
t_state)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmigrationsoperationsfields.py", line 84, in database_fo
    field,
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbbackendssqlite3schema.py", line 306, in add_field
    self._remake_table(model, create_field=field)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbbackendssqlite3schema.py", line 178, in _remake_table
    self.effective_default(create_field)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbbackendsbaseschema.py", line 240, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmodelsfieldsrelated.py", line 936, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=conne
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmodelsfields__init__.py", line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepa
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmodelsfields__init__.py", line 939, in get_db_prep_valu
    value = self.get_prep_value(value)
  File "C:UsersLiToAppDataLocalProgramsPythonPython36-32libs
sdjangodbmodelsfields__init__.py", line 947, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: '??? ??? ?????'
Asked By: Ahmed Wagdi

||

Answers:

Error message: ValueError: invalid literal for int() with base 10: '??? ??? ?????'

As per exception int() with base 10: '??? ??? ?????' doesn’t qualify as int.
Check in blog.0001_initial migrations for '??? ??? ?????' and modify that value with a valid int.

You might have accidentally provided garbage default value while rerunning makemigrations command which isn’t an int()

Answered By: Anubhav Singh

I had the same error.

My problem:

I had:

models.y
expiration_date = models.DateField(null=True, max_length=20)

And a validation in my:

forms.py
    
def clean_expiration_date(self):
    data = self.cleaned_data['expiration_date']
    if data < datetime.date.today():
return data

And it gave me the here mentioned error. But only on a certain page.

My solution:

I changed the DateField to DateTimeField

And it fixed my problem in my case.

Answered By: Marcel B
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.