类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“datetime.datetime”

2024-01-05

我试图为我的所有模型添加“created_at”字段,但收到以下错误...

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

按照顺序,这是我的工作流程......

1)我把created_at = models.DateTimeField(auto_now_add=True)在我的模型中。
2)我跑python manage.py makemigrations它在我的命令行中显示以下提示...

You are trying to add a non-nullable field 'created_at' to comment without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()

3)此时我输入timezone.now()在所有模型上,完成后它成功地为我创建了迁移文件。

4)我跑python manage.py migrate并得到TypeError上面显示的消息。

我尝试过的事情...

1)通过Stack Overflow查找类似问题(无济于事)...
2)删除migrations文件并尝试使用datetime.datetime.now()
3)删除迁移文件并尝试使用简单整数1.

所有这些尝试都没有结果。

完整的命令行回溯...

(env)alopex@Alopex:~/repos/hacker_news$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, sessions, hackernews, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying hackernews.0003_auto_20151226_1701...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 382, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
    default_value = self.effective_default(field)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/models/fields/related.py", line 910, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/alopex/repos/hacker_news/env/lib/python3.4/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'

完整迁移文件...

# -*- coding: utf-8 -*-
# Generated by Django 1.9 on 2015-12-26 17:01
from __future__ import unicode_literals

import datetime
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc


class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('hackernews', '0002_auto_20151224_1605'),
]

operations = [
    migrations.AddField(
        model_name='comment',
        name='created_at',
        field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2015, 12, 26, 17, 1, 23, 211181, tzinfo=utc)),
        preserve_default=False,
    ),
    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,
    ),
    migrations.AddField(
        model_name='commentvote',
        name='created_at',
        field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2015, 12, 26, 17, 1, 34, 85491, tzinfo=utc)),
        preserve_default=False,
    ),
    migrations.AddField(
        model_name='post',
        name='created_at',
        field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2015, 12, 26, 17, 1, 37, 779457, tzinfo=utc)),
        preserve_default=False,
    ),
    migrations.AddField(
        model_name='postvote',
        name='created_at',
        field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2015, 12, 26, 17, 1, 41, 794803, tzinfo=utc)),
        preserve_default=False,
    ),
]

问题出在新的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,
),

显然默认值是错误的。我认为您错误地插入了它。

您应该指定用户的主键,或者给出User object.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“datetime.datetime” 的相关文章

随机推荐

  • Flash 不支持 URL 中的用户名/密码吗?我有什么选择?

    我一直在尝试从开头带有用户名 密码的 URL 加载一些 JSON 所以 URL 类似于 http 用户名 电子邮件受保护 api 配置文件 http username password www myaddress org uk api pr
  • 属性编辑器设计模式?

    Warning 这是超级深入的 我理解 如果你根本不想读这篇文章 这主要是为了我整理一下我的思维过程 好的 这就是我想做的 我有这些对象 当您单击一个 或选择多个 时 它应该在右侧显示它们的属性 如图所示 当您编辑所述属性时 它应该立即更新
  • subclipse:无法加载默认 SVN 客户端

    使用 eclispe 和 subclipse 插件时 我不断收到此错误 Unable to load default SVN Client 我已经安装了 SVNKit 和 JavaHL 提供程序 但在首选项中找不到它 我正在使用 eclip
  • Amazon SES 中的特殊字符

    我正在使用适用于 PHP 的 AWS 开发工具包 https github com aws aws sdk php https github com aws aws sdk php 使用 Amazon SES 发送电子邮件 这是代码
  • 我们如何使用 C++ Lib Function 或 Windows API 在一个会话中执行多个 cmd 命令?

    在 C 中运行 cmd 命令之前 我想设置一些临时环境变量 该变量会在命令行会话结束时被删除 例如在执行下面的cmd命令之前 我想设置P4PASSWD强制环境变量 sprintf s p4Command 500 C p4 exe print
  • 将切片作为函数参数传递,并修改原始切片

    我知道 Go 中的所有内容都是按值传递的 这意味着如果我给一个函数一个切片 并且该函数使用内置函数附加到该切片append函数 那么原始切片将不会具有附加到函数范围内的值 例如 nums int 1 2 3 func addToNumbs
  • Google 地图 API 2 MapView 未更新

    我在 Android 应用程序中显示 Google Maps API v2 MapView 但奇怪的是它没有以一致的方式正确更新 我正在使用 GPS 更新地图位置 尝试了 LocationManager 和 LocationClient 尽
  • 早期返回与嵌套正 if 语句

    这是一些假设的代码示例 if e KeyCode Keys Enter if this CurrentElement null return false if this CurrentElement this MasterElement r
  • 视频标签中的 YouTube URL

    我正在使用
  • 用于 WorkItemAttribute?

    我注意到 Visual Studio 测试中有一个属性 Microsoft VisualStudio TestTools UnitTesting WorkItemAttribute 可用 我正在使用 VS 2010 Premium 和 TF
  • 为什么在Java算术中,上溢或下溢永远不会抛出异常?

    在Java算术运算期间 JVM不会抛出下溢或溢出异常 很多时候我们会遇到意想不到的结果 并想知道哪里出了问题 而在 NET 技术的情况下 我们有溢出和下溢异常 所以我的问题是 为什么Java被设计成在算术运算期间不抛出这个异常 这可能是多种
  • 如何将 before_filter 应用于 Rails 3.2.11 中每个控制器的每个操作?

    我想验证用户是否在对服务器的每个请求中登录 就像是 before filter verify logged in 我应该将 before filter 放在哪里 以便它适用于所有控制器操作和所有请求 要确保过滤器适用于所有操作 请将其放置在
  • Android JQuery focus() 解决方法

    我正在开发一个 HTML5 移动应用程序 使用常规 jQuery 而不是移动应用程序 它实现了出现在文本区域下方的自定义自动完成列表 用户从列表中选择一个选项 该单词将自动完成 然后用户照常继续输入 问题在于 用户点击文本框外部以从自动完成
  • 自动装配应用程序上下文

    我正在尝试自动装配WebApplicationContext进入一个班级ImageCreatorUtil这是我在 Spring MVC 项目中创建的 在类中执行使用应用程序上下文的方法时 我总是收到 NPE 需要注意的是 该方法是由Appl
  • 在模型中使用 cakephp 组件

    如何在我的模型类之一中使用我在 cakePHP 中创建的组件 这可能吗 如果是这样 请告诉我该怎么做 这是可能的 但很漂亮不好的做法 in a MVC http en wikipedia org wiki Model E2 80 93vie
  • HTML 输出的单元测试?

    这可能是一个愚蠢的问题 但是您是否对 PHP 函数 脚本的 HTML 输出进行单元测试 我尝试将我的 HTML 和 PHP 分开 即 HTML 包含占位符 以及某些重复元素的函数 表格数据 任何类型的循环输出 但我不知道如何去验证这一点 是
  • Firebase iOS 推送通知在首次安装时不起作用

    当我第一次安装并打开应用程序并接受来自 Apple 的通知权限警报时 我从 Firebase 收到以下日志 5 16 0 Firebase InstanceID I IID023004 无法将密钥对的属性更新为首次解锁后可访问 更新 状态
  • 生产/构建中的 Material-UI 渲染错误

    我在构建我的反应应用程序时遇到了很大的问题 我正在使用material ui core v 4 10 2 在正常的反应脚本启动开发服务器上一切正常 但是 当通过 Nginx 或 npm module 构建并提供服务时 渲染无法正常工作 我在
  • 重置等待计时器

    来自 MSDN The 取消等待定时器 http msdn microsoft com en us library ms681985 aspx函数不会改变定时器的信号状态 它会在定时器设置为有信号状态之前停止定时器并取消未完成的 APC 因
  • 类型错误:int() 参数必须是字符串、类似字节的对象或数字,而不是“datetime.datetime”

    我试图为我的所有模型添加 created at 字段 但收到以下错误 TypeError int argument must be a string a bytes like object or a number not datetime