2016-05-05 26 views
0

当我尝试向数据库添加新数据时,出现值错误。Django:ValueError:int()与基数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模型的数据,我犯了一个错误。

PSAnd我只注意到:后更改我的代码,并使用“蟒蛇manage.py迁移”我得到这个错误:

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的”现在。我已经习惯了这个错误,甚至没有注意到这一点。我通常使用命令'python manage.py reset_db'从django-extensions,一切都在它之后。据我了解,我应该删除旧的迁移,不是吗?也许它可以修复我的另一个错误?

我是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* 
+0

您可以添加错误的完整跟踪,以便发现错误变得有点容易吗? – Shubhanshu

+0

好的)我已经加了 – Aska

回答

1

所以,有一两件事是肯定的。您在运行迁移时遇到的错误是因为django正在读取其中一个将进行迁移的文件,其中会有'usergame'表。尽管回收迁移有时会很痛苦(删除旧的迁移文件是一种方法),但通过运行reset_db命令,您可以实际删除数据库并重新创建它。在这种情况下,我通常会这样做,首先删除数据库,然后运行makemigrations,然后运行迁移。但是,在这种情况下,只有在确定自己的模型时才运行makemigrations。完成您的模型,运行python manage.py makemigrations并随后迁移命令。其次,您在第二段中写道,在您更改代码并运行迁移之后,您会收到错误消息。现在,由于查看了您的(修改过的)模型并将您得到的错误(无效文字)相关联,因此您很难理解错误背后的原因(因为您的模型看起来不错)。我想到的最好的理论是:因为ValueError: invalid literal for int() with base 10在接受整数的字段中出现,并且在该字段中,您正在提供浮点数。因此,要么将该字段转换为FloatField,要么在该字段中提供正确的值(我相信您必须知道这一事实,但只是为了以防万一:即使8.0000不是int,也是浮点数)。

在收回你的迁移时,可能会在新模型进入你的数据库时有希望解决问题。

+0

非常感谢你) – Aska

相关问题