Django migrate error : TypeError expected string or bytes-like object

If you modified the fields in models. after that you run makemigrations that time it asking like this

^C(api_env)[email protected]:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1

We select 1 option, then it will display like this

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

We given timezone.now() then makemigrations completed.

See your migrations last file, there is

class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('providers', '0023_remove_content_provider'),
]

operations = [
    migrations.AddField(
        model_name='content',
        name='provider',
        field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
        preserve_default=False,
    ),
]

In above code observe this line

default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)

In that line forgeinkey field default value is datetime, Is it correct value?

No, so you have to give some string or object is a default value to that field.

Now you have to edit that value in that corresponding migration file, like this

default='some string'

then save and run the migrate command.

Try and let me know, Is it works or not.Thanks

Change your model field default value for auto_now_add=True

end_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(auto_now_add=True)

Remove your migrations with these commands :

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

Then delete database file with rm db.sqlite

Then run your server

Just remove all files in migrations folder except "init.py" and delete "db.sqlite3" i.e your database and now make changes that you want and migrate.Hope This helps :)

Same issue here, when running through adding placeholders to my datefields, I accidentally entered "1" where "django.utils.timezone.now" should be, that is for the date-time default. Go into your last migration and find the "default=xxx" that is not like the rest and insert "django.utils.timezone.now"

Goto migration folder "migrations"

go to your migration file e.g"0054_comment_date.py"

change the 'default' in line field=models.DateTimeField(auto_now_add=True, default=0)

from 0 or whatevere is there

to default='2012-09-04 06:00:00.000000-08:00'

elvonkh

I encountered with the same problem. The solution was to delete all default spaces in 'migrations' folder.

while you type 'python manage.py migrate' go up in terminal and find where the exact file in 'migrations' stopping the migrating. Then find all default arguments for DateField or DateTimeField and delete it. Those models don't handle with default argument.

migrations.AlterField(
        model_name='order',
        name='start_date',
        field=models.DateField(auto_now_add=True, ***BUG default=False BUG***),
        preserve_default=False,
    ),

See the log in your terminal:

Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):

in this file there is a bug. Go there and delete the default argument from DateField

I had the same problem. Look at all the models.DateTimeField lines in the .py file generated after calling makemigrations. There you will notice that some of the DateTimeField have a wrong default value. Replace those with models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now) for example.