简体   繁体   English

ValueError:int() 的无效文字,基数为 10:'SOME STRING'

[英]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我在 cmd 中写这个“python manage.py migrate”时遇到问题我发现了这个错误

{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这是完整的回溯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}' .如果您使用的是 Python 3.6,则可以使用f'{self.whatever}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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