简体   繁体   中英

ValueError: invalid literal for int() with base 10: 'SOME STRING'

I have a problem when I write in cmd this "python manage.py migrate" I found this error

{ValueError: invalid literal for int() with base 10: 'SOME STRING'}

this is my code

class GameScore(models.Model):
    FirstTeam = models.CharField(max_length=256, null=True)
    SecondTeam = models.CharField(max_length=256, null=True)
    first_team_score = models.IntegerField(default=0)
    second_team_score = models.IntegerField(default=0)
    game_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return '{} {} - {} {}'.format(self.FirstTeam, self.first_team_score, self.SecondTeam, self.second_team_score)

this is full Traceback https://i.stack.imgur.com/RM284.png

Apply all migrations: admin, auth, contenttypes, sessions, team
Running migrations:
  Applying team.0007_auto_20180504_0343...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\core\management\commands\migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\migrations\executor.py", line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\migrations\executor.py", line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\migrations\migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\migrations\operations\fields.py", line 88, in database_forwards
    field,
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 238, in add_field
    self._remake_table(model, create_field=field)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 113, in _remake_table
    self.effective_default(create_field)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\backends\base\schema.py", line 229, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\models\fields\related.py", line 963, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\models\fields\__init__.py", line 770, in get_db_prep_save
    prepared=False)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\models\fields\__init__.py", line 958, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\haytham\Desktop\teammanager_env\lib\site-packages\django\db\models\fields\__init__.py", line 966, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'SOME STRING'

I don't know how to solve this problem

Think of the () as a list. You need numbers to tell which value to pull.

return '{0} {1} - {2} {3}'.format(self.FirstTeam, self.first_team_score, self.SecondTeam, self.second_team_score)

If you are using Python 3.6 you could use f'{self.whatever}' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM