TypeError:int()参数必须是一个字符串,类似字节的对象或数字,而不是'datetime.datetime' [英] TypeError: int() argument must be a string, a bytes-like object or a number, not 'datetime.datetime'

查看:14703
本文介绍了TypeError:int()参数必须是一个字符串,类似字节的对象或数字,而不是'datetime.datetime'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为所有的模型添加一个'created_at'字段,并且收到以下错误...

TypeError:int()参数必须是一个字符串,一个类似字节的对象或数字,而不是'datetime.datetime'

I am attempting to add a 'created_at' field for all my models and am getting the following error...

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 ,并在我的命令行中显示以下提示...

In order, this is my work flow...

1) I put created_at = models.DateTimeField(auto_now_add=True) in my models.
2) I run python manage.py makemigrations and it displays the following prompt in my command line...

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)此时,我输入时区。 now()在完成后,成功地为我创建了迁移文件。

3) At this point I type in timezone.now() on all of the models and when finished it successfully creates the migrations file for me.

4)我运行 python manage.py migrate 并获取 TypeError 消息显示在上面。

4) I run python manage.py migrate and get the TypeError message displayed above.

我尝试过的东西

1)查看堆栈溢出对于类似的问题(无效)...

2)删除迁移文件并尝试使用 datetime.datetime.now()

3)删除迁移文件并尝试使用简单的整数 1

1) Looking through Stack Overflow for similar issues (to no avail)...
2) Deleteing the migrations file and attempting to use datetime.datetime.now()
3) Deleteing the migrations file and attempting to use the simple integer 1.

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

All of these attempts have been fruitless.

完整的命令行追溯...

Full command line traceback...

(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'

完全迁移文件...

Full migration file...

# -*- 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,
),

显然,默认值是错误的。我想你错误地插入了它。

Clearly the default value is wrong. I think you have inserted it by mistake.

你应该指定一个用户的主键,或者给一个用户对象。

You should either specify the primary key of a user, or give a User object.

这篇关于TypeError:int()参数必须是一个字符串,类似字节的对象或数字,而不是'datetime.datetime'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆