Saeed Ramezani
Saeed Ramezani

Reputation: 482

expected string or bytes-like object error

when I run the migrate command I see that error. I don't know it comes from which my python files. I check my models with a new database but not diff. do you have any idea? Does it come from my models.py? my templates? my views? somewhere?

python.exe .\manage.py migrate
 Operations to perform:
 Apply all migrations: admin, auth, contenttypes, matab, sessions
Running migrations:

  Applying matab.0032_auto_20190825_1010...Traceback (most recent call last):
  File ".\manage.py", line 21, in <module>
    main()
  File ".\manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\migrate.py", line 234, in
handle
    fake_initial=fake_initial,
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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\Rahkar\AppData\Local\Programs\Python\Python36\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\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\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\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\operations\fields.py", line 112, in database_forwards
    field,
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 433, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 161, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1273, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1268, in get_prep_value
    return self.to_python(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\models\fields\__init__.py", line 1230, in to_python
    parsed = parse_date(value)
  File "C:\Users\Rahkar\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\dateparse.py", line 74, in parse_date
    match = date_re.match(value)
TypeError: expected string or bytes-like object

and here is my 0032_auto_20190825_1010 file:

Generated by Django 2.2.3 on 2019-08-25 05:40

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('matab', '0031_auto_20190819_1304'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='turn',
            name='Patient',
        ),
        migrations.AddField(
            model_name='turn',
            name='Date',
            field=models.DateField(default=1),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Patient_FirstName',
            field=models.CharField(default=1, max_length=100),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Patient_LastName',
            field=models.CharField(default=1, max_length=100),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='turn',
            name='Section',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='matab.Section'),
        ),
        migrations.AlterField(
            model_name='personalnfo',
            name='EducationLevel',
            field=models.CharField(blank=True, max_length=100, null=True),
        ),
        migrations.AlterField(
            model_name='turn',
            name='Doctor',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='matab.Doctor'),
        ),
        migrations.AlterField(
            model_name='turn',
            name='Time',
            field=models.TimeField(),
        ),
    ]

Upvotes: 0

Views: 1111

Answers (2)

Nalin Dobhal
Nalin Dobhal

Reputation: 2342

In your turn model, there is a field Date, you need to provide a default date to that field.

Try this

from datetime import date

Date = models.DateField(default=date.today)

and as mentioned by @shafik, you need to delete your migration file i.e.

0032_auto_20190825_1010.py

Upvotes: 3

shafikshaon
shafikshaon

Reputation: 6404

If your turn model you set Date default value to 1. That's why the error thrown.

To solve this, change the default to timezone.now(). The existing created migration file (added in your question) need to delete to run makemigrations command successfully.

Upvotes: 1

Related Questions