返回float(value)ValueError:在Django模型中无法将字符串转换为float [英] return float(value) ValueError: could not convert string to float in Django models
问题描述
我尝试在此Django模型上进行迁移/迁移:
从django.db导入模型
从myapp .models导入站点
类GscElement(models.Model):
ctr = models.FloatField('Taux de clic',默认= 0.0)
印象= models.IntegerField( 'Nombre d\'impressions',默认值= 0)
位置= models.FloatField('Position moyenne',默认值= 0.0)
点击次数= models.IntegerField('Nombre de clics',默认值= 0)
site = models.ForeignKey(
Site,
models.SET_NULL,
blank = True,
null = True
)
类Page(GscElement):
page_field = models.TextField('Url de la page',default ='')
startdate = models.DateField('Date du首发日期',null = True)
enddate = models.DateField('Date de fin',null = True)
class Meta:
unique_together =((('startdate','enddate',' page_field',))
类别查询(GscElement):
que ry_field = models.TextField('Requête',default ='')
startdate = models.DateField('Date du premier',null = True)
enddate = models.DateField('Date de fin' ,null = True)
class Meta:
unique_together =(('startdate','enddate','query_field'),)
,我得到这个错误:
正在应用gsc。 0004_auto_20171024_1916 ...跟踪(最近一次通话最近):
文件 manage.py,第22行,在< module>中
execute_from_command_line(sys.argv)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/__init__.py,行364 ,在execute_from_command_line
utility.execute()
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/__init__.py中,第356行,在执行
self.fetch_command(subcommand).run_from_argv(self.argv)
File /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django /core/management/base.py,第283行,位于run_from_argv
self.execute(* args,** cmd_options)
文件 / Users / Quentin / git / myapp / myapp_env / lib / python3 .6 / site-packages / django / core / management / base.py,行330,在执行
输出= self.handle(* args,** options)
文件 / Users / Quentin /git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/commands/migrate.py,第204行,句柄
fake_initial = fake_initial,
文件 /用户/ Quentin / git / myapp / myap p_env / lib / python3.6 / site-packages / django / db / migrations / executor.py,行115,在迁移
状态= self._migrate_all_forwards(状态,计划,full_plan,fake = fake,fake_initial = fake_initial)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/migrations/executor.py,第145行,处于_migrate_all_forwards
状态= self.apply_migration(状态,迁移,fake = fake,fake_initial = fake_initial)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/migrations/ executor.py,第244行,位于apply_migration
state = migration.apply(state,schema_editor)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages /django/db/migrations/migration.py,第129行,应用
operation.database_forwards(self.app_label,schema_editor,old_state,project_state)
文件 / Users / Quentin / git / myapp / myapp_env / lib / python3.6 / site-packages / django / db / migrations / operations / fields.py,第87行,在database_for中病房
字段,
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py,第238行,在add_field
self._remake_table(model,create_field = field)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/backends/sqlite3 /schema.py,第113行,位于_remake_table
self.effective_default(create_field)
文件 /Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/ db / backends / base / schema.py,第229行,位于valid_default
中,默认= field.get_db_prep_save(默认,self.connection)
文件 / Users / Quentin / git / myapp / myapp_env / lib /python3.6/site-packages/django/db/models/fields/__init__.py,行770,在get_db_prep_save中
prepare = False)
文件 / Users / Quentin / git / myapp / myapp_env / lib / python3.6 / site-packages / django / db / models / fields / __ init__.py,第762行,位于get_db_prep_value
value = self.get_prep_value(value)
File / Users /昆汀/ git / myapp / myapp_env / lib / python3.6 / site-packages / django / db / models / fields / __ init__.py,行1781,在get_prep_value
return float(value)
ValueError中:不将字符串转换为float:
您知道为什么吗?
仅供参考,我尝试恢复模型更改之前的数据库,并尝试压缩迁移,但始终会发生相同的错误。
谢谢!
编辑:我粘贴了所有回溯
我不能肯定地说不看迁移文件。发布这些信息会有所帮助。也就是说,您似乎试图将ctr或position从字符串字段重命名为float字段。
这可能是在您执行makemigrations并询问您是否您重命名了一个字段。如果您不关心这些字段中的数据,则可以执行以下操作:
- 删除该最新迁移
- 再次恢复数据库
- 再次运行python manage.py makemigrations
- 当询问您是否重命名字段时,说
Django无法将字符串隐式转换为浮点数,因此如果您确实需要保留当前存储在您的数据中的模型,我建议如下:
- 创建新的浮动字段2
- 运行管理命令从一个字段获取数据,将其转换为float,然后将其保存到另一个字段
- 删除第一个(字符串)字段
- 将第二个字段(浮点字段)重命名为您需要的名称。
I try to makemigrations / migrate on this Django models :
from django.db import models
from myapp.models import Site
class GscElement(models.Model):
ctr = models.FloatField('Taux de clic', default=0.0)
impressions = models.IntegerField('Nombre d\'impressions', default=0)
position = models.FloatField('Position moyenne', default=0.0)
clicks = models.IntegerField('Nombre de clics', default=0)
site = models.ForeignKey(
Site,
models.SET_NULL,
blank=True,
null=True
)
class Page(GscElement):
page_field = models.TextField('Url de la page', default='')
startdate = models.DateField('Date du debut', null=True)
enddate = models.DateField('Date de fin', null=True)
class Meta:
unique_together = (('startdate', 'enddate', 'page_field',))
class Query(GscElement):
query_field = models.TextField('Requête', default='')
startdate = models.DateField('Date du debut', null=True)
enddate = models.DateField('Date de fin', null=True)
class Meta:
unique_together = (('startdate', 'enddate', 'query_field'),)
and I get this error :
Applying gsc.0004_auto_20171024_1916...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/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 "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/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 "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 87, in database_forwards
field,
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 238, in add_field
self._remake_table(model, create_field=field)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/backends/sqlite3/schema.py", line 113, in _remake_table
self.effective_default(create_field)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 229, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 770, in get_db_prep_save
prepared=False)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 762, in get_db_prep_value
value = self.get_prep_value(value)
File "/Users/Quentin/git/myapp/myapp_env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1781, in get_prep_value
return float(value)
ValueError: could not convert string to float:
Do you have any idea why ?
FYI I tried to restore the databased I had before my models changes and tried to squash migrations too, but the same error always occured.
Thank you !
EDIT : I pasted all the traceback
I can't say for sure without seeing the migration files. Posting those would be helpful. That said, it looks like you tried to rename either ctr or position from a string field to a float field.
This probably happened when you ran makemigrations and it asked you if you renamed a field. If you don't care about the data in those fields, you can do the following:
- Delete that latest migration
- Restore the database again
- Run python manage.py makemigrations again
- When it asks you if you renamed a field, say no.
Django can't implicitly convert a string to a float, so if you do need to keep the data currently stored in your models, I'd suggest the following:
- Creating a new float field2
- Run a management command to get the data from one field, convert it to a float, and save it to the other field
- Delete the first (string) field
- Rename the second field (the float field) to whatever name you need it to have.
这篇关于返回float(value)ValueError:在Django模型中无法将字符串转换为float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!