gpt4 book ai didi

python - 返回 float (值)ValueError : could not convert string to float in Django models

转载 作者:太空宇宙 更新时间:2023-11-04 02:39:31 24 4
gpt4 key购买 nike

我尝试在这个 Django 模型上进行迁移/迁移:

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'),)

我得到这个错误:

 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:

你知道为什么吗?

仅供引用,我尝试恢复我在模型更改之前拥有的数据库,并尝试压缩迁移,但同样的错误总是发生。

谢谢!

编辑:我粘贴了所有回溯

最佳答案

如果没有看到迁移文件,我不能肯定地说。发布这些会有所帮助。也就是说,您似乎试图将 ctr 或 position 从字符串字段重命名为浮点字段。

这可能发生在您运行 makemigrations 时,它询问您是否重命名了一个字段。如果您不关心那些字段中的数据,您可以执行以下操作:

  1. 删除最新的迁移
  2. 再次恢复数据库
  3. 再次运行 python manage.py makemigrations
  4. 当它询问您是否重命名字段时,请回答“否”。

Django 无法将字符串隐式转换为 float ,因此如果您确实需要保留当前存储在模型中的数据,我建议如下:

  1. 创建一个新的 float 字段2
  2. 运行管理命令从一个字段中获取数据,将其转换为 float ,并将其保存到另一个字段
  3. 删除第一个(字符串)字段
  4. 将第二个字段( float 字段)重命名为您需要的任何名称。

关于python - 返回 float (值)ValueError : could not convert string to float in Django models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46919009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com