TypeError:int()参数必须是字符串或数字,而不是'datetime.datetime'

时间:2022-09-11 17:57:49

I have made App12/models.py module as:

我将App12 / models.py模块设为:

from django.db import models

class Question(models.Model):

    ques_text=models.CharField(max_length=300)
    pub_date=models.DateTimeField('Published date')

    def __str__(self):
        return self.ques_text

class Choice(models.Model):

    # question=models.ForeignKey(Question)
    choice_text=models.CharField(max_length=300)
    votes=models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

Then i run the cmds

然后我运行cmds

 python manage.py makemigrations App12
 python manage.py migrate

and then enter 2 records in the Question model as:

然后在问题模型中输入2条记录:

Question.objects.create(ques_text="How are you?",pub_date='timezone.now()') 
                 # and (ques_text="What are you doing?",pub_date='timezone.now()')

Then i realise that Question and Choice models should be in foreign key relation and uncomment the above commented statement in the models code

然后我意识到问题和选择模型应该是外键关系,并在模型代码中取消注释上面注释的语句

When i run the "python manage.py makemigrations App12", it is running fine but after that, i am getting the

当我运行“python manage.py makemigrations App12”时,它运行正常,但在那之后,我得到了

"TypeError: int() argument must be a string or a number, not 'datetime.datetime"

error when i am running "python manage.py migrate" command.

我正在运行“python manage.py migrate”命令时出错。

Can anybody help me.How can i add a foreignkey relation between the Choice model and Question model now.

任何人都可以帮助我。如何在Choice模型和问题模型之间添加外键关系。

2 个解决方案

#1


13  

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.

从您的迁移文件中,您收到此错误是正常的,您正在尝试将日期时间存储在需要为int的Foreignkey上。

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

当迁移询问您将为旧的Choice行设置哪个值时会发生这种情况,因为需要新的ForeignKey。

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.

要解决此问题,您可以更改迁移文件并将datetime.date ...更改为Question表中的有效ID,如下面的代码所示。或者删除迁移文件并重新运行./manage.py makemigrations,当您被问及默认值时是否提示有效的问题ID,而不是日期时间。

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, ),
    ]

#2


2  

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

pub_date不应该是一个字符串。按如下方式创建对象:

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

#1


13  

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.

从您的迁移文件中,您收到此错误是正常的,您正在尝试将日期时间存储在需要为int的Foreignkey上。

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

当迁移询问您将为旧的Choice行设置哪个值时会发生这种情况,因为需要新的ForeignKey。

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.

要解决此问题,您可以更改迁移文件并将datetime.date ...更改为Question表中的有效ID,如下面的代码所示。或者删除迁移文件并重新运行./manage.py makemigrations,当您被问及默认值时是否提示有效的问题ID,而不是日期时间。

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, ),
    ]

#2


2  

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

pub_date不应该是一个字符串。按如下方式创建对象:

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