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

时间:2018-05-04 12:58:22

标签: python django

我在cmd写这个" python manage.py migrate" 我发现了这个错误

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

这是我的代码

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)

这是完整的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'

我不知道如何解决这个问题

1 个答案:

答案 0 :(得分:0)

()视为列表。你需要数字来判断要拉出哪个值。

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

如果您使用的是Python 3.6,则可以使用f’{self.whatever}’