Python Django`migrate`命令失败,原因是:ValueError:int()的文本无效,以10为基数:''&引用;IntegerField";
你能给我解释一下吗?为什么这是我的模特?我收到以下错误消息:ValueError:以10为基数的int()的文本无效:“”Python Django`migrate`命令失败,原因是:ValueError:int()的文本无效,以10为基数:''&引用;IntegerField";,python,django,django-models,Python,Django,Django Models,你能给我解释一下吗?为什么这是我的模特?我收到以下错误消息:ValueError:以10为基数的int()的文本无效:“” 您正试图通过int()模型中的某个地方将字符串转换为整数。IntegerField失败,因为int()不知道如何处理字符串 要解决此问题,请确保您的数据输入都是整数或数字。您在评论中发布了以下日志: >python manage.py migrate Operations to perform: Apply all migrations: admin, auth,
您正试图通过
int()
模型中的某个地方将字符串转换为整数。IntegerField失败,因为int()
不知道如何处理字符串
要解决此问题,请确保您的数据输入都是整数或数字。您在评论中发布了以下日志:
>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contacts, contenttypes, sessions
Running migrations:
Applying contacts.0003_contact_nomor_hp...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
output = self.handle(*args, **options)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
field,
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\schema.py", line 42, in add_field
super().add_field(model, field)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 421, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 168, in column_sql
default_value = self.effective_default(field)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 785, in get_db_prep_value
value = self.get_prep_value(value)
File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 1807, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''
>python manage.py迁移
要执行的操作:
应用所有迁移:管理、身份验证、联系人、内容类型、会话
运行迁移:
正在应用contacts.0003\u contact\u nomor\u hp…回溯(最近一次呼叫最后一次):
文件“manage.py”,第15行,在
从命令行(sys.argv)执行命令
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\\ uuuuuuuuuuuuuuuuuu init.py”,第381行,从命令行执行
utility.execute()
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\\ uuuuuu init\uuuuuu.py”,第375行,在execute中
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\base.py”,第316行,从\u argv运行
self.execute(*args,**cmd_选项)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\base.py”,第353行,执行
输出=self.handle(*args,**选项)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\base.py”,第83行,以包装形式
res=句柄函数(*args,**kwargs)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\core\management\commands\migrate.py”,第203行,位于句柄中
假首字母=假首字母,
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\migrations\executor.py”,第117行,在migrate中
状态=self.\u迁移\u所有\u转发(状态,计划,完整计划,假=假,假首字母=假首字母)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\migrations\executor.py”,第147行,全部向前迁移
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\migrations\executor.py”,第244行,在apply\u migration中
state=migration.apply(状态,模式编辑器)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\migrations\migration.py”,第124行,应用
操作。数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)
数据库中第84行的文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\migrations\operations\fields.py”
领域
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\backends\mysql\schema.py”,第42行,在add\u字段中
super().添加_字段(模型,字段)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\backends\base\schema.py”,第421行,在add\u字段中
定义,params=self.column\u sql(模型,字段,include\u默认值=True)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\backends\base\schema.py”,第168行,sql列
默认值=自生效默认值(字段)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\backends\base\schema.py”,第239行,默认有效
返回字段.get\u db\u prep\u save(默认,self.connection)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\models\fields\\ uuu init\uuu.py”,第790行,位于get\u db\u prep\u save中
返回self.get_db_prep_值(值,connection=connection,prepared=False)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\models\fields\\ uuuu init\uuuuu.py”,第785行,位于get\u db\u prep\u值中
value=self.get\u prep\u值(value)
文件“C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site packages\django\db\models\fields\\ uuuuu init\uuuuu.py”,第1807行,在get\u prep\u值中
返回int(值)
ValueError:基数为10的int()的文本无效:“”
问题可能是数据库中已有一些数据,因此无法添加列nomor\u hp
这里有两个选项:
IntegerField(默认值=0)
)IntegerField(null=True,blank=True)
)我怀疑发生的是这件事。您在您的
联系人
型号中添加了nomor\u hp
字段。然后运行makemigrations
,得到如下输出:
You are trying to add a non-nullable field 'num' to foo without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
您可能选择了1(如上所述),然后只需按enter键或'
。由于'
不是整数,因此迁移
会因您发布的消息而失败。要做的是:
1) 将Contact.nomor\u hp字段固定到此nomor\u hp=models.IntegerField(默认值=None)
2) 再次运行makemigrations
3) 编辑有问题的迁移文件(您可以在跟踪输出中找到),在您的情况下,它是0003\u contact\u nomor\u hp.py
文件。在该文件中,您应该更改
operations = [
migrations.AddField(
model_name='contact',
name='nomor_hp',
field=models.IntegerField(default=''),
preserve_default=False,
),
]
…到
operations = [
migrations.AddField(
model_name='contact',
name='nomor_hp',
field=models.IntegerField(default=0),
preserve_default=False,
),
]
4) 运行migrate
那你应该很好。你的模特看起来不错。您能发布错误的完整跟踪吗?问题不在于此代码。显示使用此模型的零件,以及
operations = [
migrations.AddField(
model_name='contact',
name='nomor_hp',
field=models.IntegerField(default=0),
preserve_default=False,
),
]