Django - Errors Solving

stackpython
3 min readApr 15, 2020

I decide to write this article for the main purpose to collect solved problems of errors occuring during Django development project.

Error 1:

TypeError: Field ‘id’ expected a number but got datetime.datetime(2020, 3, 14, 8, 28, 56, 265918, tzinfo=<UTC>)

Question: What happened ?

I need to add the new fields showed in the picture below(user and cover_pic) in models.py file. The first one(user) I wanna make relationship between Post table and User table. Of course this field(user) requires ForeignKey, but it isn’t the point, and the last one is cover_pic field used to upload images to show a featured image for my blog post.

I added 2 fields; user and cover_pic in Post table

Problem: When running the command “python manage.py migrate” I suddenly got the error “TypeError: Field ‘id’ expected a number but got datetime.datetime…

the error I got

Then I decide to browse to migrations directory containing 3 files; 0001_initial.py, 0002_auto_2020.., and the last one __init__.py on the left hand side, just wanna make sure what happened in these fields. I Clicked on 0002_auto_2020.. file

migrations directory

Now I can see what happened. Focusing on the first parameter of each field, it stores datetime value. Indeed, it should not be datetime, it should be an integer value instead.

So I removed django.utils.timezone.now, then replace 1 to the default parameter → default=1.

run → python manage.py migrate

Job done!! No worries anymore, the problem is gone away. I can sleep well now ;)

If you like this article, do not forget to hit the clap to support my work, or you have any suggestions, do not hesitate to drop your comment to the comment section below.

See you next error !!

This article is written by: [Sonny STACKPYTHON]

--

--