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

16👍

From your migration file it’s normal that you get this error, you are trying to store a datetime on a Foreignkey which need to be an int.

This is happened when the migration asked you which value will be set for old Choice rows because the new ForeignKey is required.

To resolve it, you can change the migration file and change the datetime.date… to a valid id from the Question table like the code bellow. Or delete the migration file and re-run ./manage.py makemigrations, when you will be asked about the default value prompt a valid Question id, not a datetime.

from future import unicode_literals
from django.db import models, migrations
import datetime

class Migration(migrations.Migration):
    dependencies = [ ('App11', '0003_remove_choice_question'), ]
    operations = [
        migrations.AddField(
            model_name='choice',
            name='question',
            field=models.ForeignKey(default=1, to='App11.Question'), preserve_default=False, ),
    ]
👤Mounir

2👍

pub_date should not be a string. Create your object as follows:

from django.utils import timezone
Question.objects.create(ques_text="How are you?",pub_date=timezone.now()) 

Leave a comment