当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《TypeError:int()参数必须是字符串,类字节对象或数字,而不是'datetime.datetime'》经验,为你挑选了1个好方法。

我正在尝试为我的所有模型添加'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)删除迁移文件并尝试使用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 
    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,
    ),
]

Andrea Corbe.. 8

问题出在新的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对象.



1> Andrea Corbe..:

问题出在新的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对象.

推荐阅读
雨天是最美
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有