Python 类型错误:int() 参数必须是字符串或数字,而不是“datetime.datetime”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32440251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:34:28  来源:igfitidea点击:

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

pythondjangodjango-orm

提问by Jagat

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:

然后在 Question 模型中输入 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.

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

采纳答案by Mounir

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 的外键上。

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

回答by Régis B.

pub_dateshould 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())