Quentin
Quentin

Reputation: 693

return float(value) ValueError: could not convert string to float in Django models

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

Upvotes: 2

Views: 1463

Answers (1)

Robert Townley
Robert Townley

Reputation: 3564

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:

  1. Delete that latest migration
  2. Restore the database again
  3. Run python manage.py makemigrations again
  4. 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:

  1. Creating a new float field2
  2. Run a management command to get the data from one field, convert it to a float, and save it to the other field
  3. Delete the first (string) field
  4. Rename the second field (the float field) to whatever name you need it to have.

Upvotes: 2

Related Questions