TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'

The problem is with the new ForeignKey:

migrations.AddField(
    model_name='comment',
    name='user',
    field=models.ForeignKey(default=datetime.datetime(2015, 12, 26, 17, 1, 28, 128127, tzinfo=utc), on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
    preserve_default=False,
),

Clearly the default value is wrong. I think you have inserted it by mistake.

You should either specify the primary key of a user, or give a User object.


I fixed this in a very clean way. Here is what you need to do

  • Go to migrations folder and search for the migration file of the model you want to migrate. you will find that, it has a default set to timezone.now

  • Change that to no default(remove the default config) and set null=True blank=True

  • Run python manage.py migrate

And that should fix it.


None of those solutions work for me. But when I removed all files from migrations folder in my app folder in project and ran python manage.py migrate, everything worked fine and there were no more problems.