繁体   English   中英

Django:ValueError:以10为底的int()的无效文字

[英]Django:ValueError: invalid literal for int() with base 10

我在尝试向数据库中添加新数据时遇到值错误。

我的models.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User

DEFAULT_ROLE_ID=1
class UserProfile(models.Model):
  user= models.OneToOneField(User)
  avatar= models.ImageField(upload_to='Images/users', verbose_name='Image')
  rating=models.IntegerField(default=0)
  karma=models.IntegerField(default=0)

  def __unicode__(self):
    return unicode(self.user)

class Room(models.Model):
  title=models.CharField(max_length=63,default='test')
  time_creation=models.DateTimeField('Time of room creation')
  users=models.ManyToManyField(UserProfile)

  def __unicode__(self):
    return unicode(self.title)

class Game(models.Model):
  title=models.CharField(max_length=63,default='test')
  time_creation=models.DateTimeField('Время создания')
  room=models.ForeignKey(Room)

  def __unicode__(self):
    return unicode(self.title)

class SecretWord(models.Model):
  word=models.CharField(max_length=255)
  game=models.ForeignKey(Game)

  def __unicode__(self):
    return unicode(self.word)

class UserRole(models.Model):
  PLAYER = 'PL'
  LIDER = 'LID'
  ROLE_CHOICES = (        
    (LIDER, 'Lider'),
    (PLAYER, 'Player'),
  )
  user=models.ForeignKey(UserProfile,default=1)
  game=models.ForeignKey(Game,default=1)
  role = models.CharField(max_length=3,choices=ROLE_CHOICES,default=LIDER)

  def __unicode__(self):
    return unicode(self.role)    

class Message(models.Model):
  text=models.TextField(max_length=2047)
  time_creation=models.DateTimeField('Time of room creation')
  room=models.ForeignKey(Room)
  user=models.ForeignKey(UserProfile)
  user_role=models.ForeignKey(UserRole, default=DEFAULT_ROLE_ID)

  def __unicode__(self):
    return unicode(self.text)

错误是:

  Applying hat.0030_auto_20160421_1632...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/mysql/schema.py", line 50, in add_field
    super(DatabaseSchemaEditor, self).add_field(model, field)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 382, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
    default_value = self.effective_default(field)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 915, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'Player'

我试图注释掉代码的不同部分,但是甚至不知道在哪里使用UserRole模型中的数据,这是一个错误。

PS我刚刚注意到:更改代码并使用“ python manage.py migration”后,出现此错误:

Operations to perform:
Apply all migrations: admin, contenttypes, hat, auth, sessions
Running migrations:
  Applying hat.0030_auto_20160421_1632...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 284, in create_model
    self.execute(sql, params or None)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 110, in execute
    cursor.execute(sql, params)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
    return self.cursor.execute(query, args)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/aska1/prog/prog/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1050, "Table 'hat_usergame' already exists")

但是表“ usergame”现在有了另一个名称-“ UserRole”。 我对这种错误非常习惯,甚至没有注意到这一点。 我通常使用django-extensions中的命令“ python manage.py reset_db”,之后一切正常。 据我了解,我应该删除旧的迁移,不是吗? 也许它可以解决我的另一个错误?

我是Django的新手,所以也许我根本不了解基本知识。


结果,当一切都出现问题时,通过删除从迁移号0030开始的迁移,解决了该问题。

查看迁移列表:

python manage.py migrate --list
app
(*) 0001_initial
(*) 0002_auto__...
(*) 0003_auto__...
(*) 0004_auto__...

应用上一次正确的迁移并删除以下内容:

./manage.py migrate app 0003
rm app/migrations/0004*

因此,有一件事是肯定的。 您在运行迁移时遇到的错误是因为django正在读取makemigrations会生成的文件之一,并且其中会有'usergame'表。 虽然有时撤回迁移可能很痛苦(删除旧迁移文件是一种方法),但是通过运行reset_db命令,您实际上是在删除数据库并重新创建它。 在这种情况下,我通常会做的是,首先删除数据库,然后运行makemigrations,然后运行迁移。 但是,在这种情况下,仅在对模型有把握时才运行makemigrations。 最终确定模型,运行python manage.py makemigrations并随后执行迁移命令。

其次,您在第二段中写道,更改代码并运行迁移后,您将得到错误。 现在,由于查看了(修改过的)模型并关联了所得到的错误(无效的文字),因此很难理解错误的原因(因为模型看起来不错)。 我想出的最好的理论是:由于ValueError: invalid literal for int() with base 10出现在接受整数的字段中,并且在该字段中提供浮点数。 因此,可以将该字段转换为FloatField或在该字段中提供正确的值(我确信您必须了解这一事实,但只是以防万一:即使8.0000也不是整数,它是一个浮点数)。

尝试撤消迁移,并且有可能在新模型进入数据库时​​希望该问题可以解决。

暂无
暂无

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

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