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

8👍

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.

6👍

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.

4👍

I have had a similar problem and I would like to share how I solved this as it may be of benefit to others. This was a challenge to solve with little specific information from the Django documentation and this thread appears to be the best-phrased question on stackoverflow.

My situation:

  • I am new to Django and experimenting, and using the SQLLite database model
  • I had created a simple model, and during the migrate is asked the question about providing a one-off default, which I provided
  • From this point on the migrate process would not forget this default, and I could not migrate this further and was stuck

My solution:

  • Given that I am not in production and also not in large-scale development I adopted the approach to kill the database and all of the migrations and then to migrate again, which solved the problem
  • NOTE: If you have vested data in the database, then back up the data first or seek other guidance – there are other threads which show how to do this backup and restore, even between database management systems
  • under the project root, look for the file db.sqlite3 and rename or delete this which will force this to be recreated on the next migrate
  • under the migrations folder delete ALL of the migrations .py files – this will prevent these from being applied again when the database is recreated
  • run migrate again – which will create a clean database without attempting to reinstate the default

I accept that my approach is a bit of a hack but it worked for me, and also without any damage to my models which were all created perfectly – I did also fix the lack of the default which was causing the problem which was a mistake of mine in entering a char default for an integer field. However, there may be side-effects if you are further into your development and I would welcome suggestions on a safer approach to this problem.

3👍

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.

👤Ghasem

2👍

I have faced the same problem, I have tried as suggested above and I deleted only my migration file which is causing this issue (latest file created while applying python manage.py makemigrations), again I run python manage.py makemigrations, here I notice that previously I have given >>> timezone.now() as default for ForeignKey Field, this time I have set default value in my models.py file and my problem was solved.

Note: don’t provide default values for ForeignKeys while migrating.

0👍

For me it worked when i opened not migrated file and on ForeignKey i separated the values null=True, blank=True with the comma in between.

field=models.ForeignKey(null=True, blank=True, on_delete=django.db.models.deletion.CASCADE, to=’your_app.Model’)

👤Lukasz

Leave a comment