Home > Software design >  why I am having TypeError: expected string or bytes-like object - django
why I am having TypeError: expected string or bytes-like object - django

Time:12-31

hello I am doing an app with django and I was using sqlite I want to migrate to postgres now but I am having this error I think something related to time but I really don't know how to solve it.

Applying users.0018_auto_20210911_1322...Traceback (most recent call last):
  File "C:\Users\lenovo ideapad\Desktop\callservices\manage.py", line 22, in <module>
    main()
  File "C:\Users\lenovo ideapad\Desktop\callservices\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\base.py",
line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\base.py",
line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\base.py",
line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\core\management\commands\migrate.py", line 244, in handle
    post_migrate_state = executor.migrate(
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\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\lenovo ideapad\Desktop\callservices\env\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\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\migrations\executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\migrations\migration.py", line 126, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\migrations\operations\fields.py", line 104, in database_forwards
    schema_editor.add_field(
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\backends\base\schema.py", line 490, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\backends\base\schema.py", line 237, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\backends\base\schema.py", line 324, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\models\fields\__init__.py", line 842, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\models\fields\__init__.py", line 2296, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\models\fields\__init__.py", line 2291, in get_prep_value
    return self.to_python(value)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\db\models\fields\__init__.py", line 2265, in to_python
    parsed = parse_time(value)
  File "C:\Users\lenovo ideapad\Desktop\callservices\env\lib\site-packages\django\utils\dateparse.py", line
90, in parse_time
    match = time_re.match(value)
TypeError: expected string or bytes-like object

this is the full code of users.0018_auto_20210911_1322 I don't know how to solve it yet I tried somethings but doesn't work

class Migration(migrations.Migration):
    dependencies = [
        ('users', '0017_alter_profile_conception'),
    ]
    operations = [
        migrations.AddField(
            model_name='profile',
            name='periodfrom',
            field=models.TimeField(default=1),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='profile',
            name='periodto',
            field=models.TimeField(default=1),
            preserve_default=False,
        ),
    ]

CodePudding user response:

You had misconfigured periodfrom and periodto fields at Profile model. Change it this way:

periodfrom = models.TimeField(default=datetime.time(10, 00))
periodto = models.TimeField(default=datetime.time(20, 00))

Also remove 0018_auto_20210911_1322 migration and do makemigrations again.

  • Related