【问题标题】:invalid literal for int() with base 10: 'pk' Python/Django以 10 为底的 int() 的无效文字:'pk' Python/Django
【发布时间】:2015-08-19 15:07:07
【问题描述】:

我正在 Django 中处理一个项目,并且刚刚在我的一个模型中添加了一个字段,以保存创建它的用户。我将它的默认值设置为“无”。现在,每当我尝试迁移时,都会收到一个值错误,提示“invalid literal for int() with base 10: 'pk'”。这是我的模型的样子:

class SampleModel(models.Model):
    created_by = models.ForeignKey(User, default=None)
    # other fields here

这是我尝试运行 python manage.py migrate 时得到的回溯:

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, posts, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
   Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying posts.0002_post_created_by...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
  File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
  File "/Library/Python/2.7/site-packages/django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Python/2.7/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
  File "/Library/Python/2.7/site-packages/django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Python/2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 179, in add_field
self._remake_table(model, create_fields=[field])
  File "/Library/Python/2.7/site-packages/django/db/backends/sqlite3/schema.py", line 77, in _remake_table
self.effective_default(field)
  File "/Library/Python/2.7/site-packages/django/db/backends/base/schema.py", line 211, in effective_default
default = field.get_db_prep_save(default, self.connection)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py", line 1956, in get_db_prep_save
return self.related_field.get_db_prep_save(value, connection=connection)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 710, in get_db_prep_save
prepared=False)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 977, in get_db_prep_value
value = self.get_prep_value(value)
  File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'pk'

现在我认为这与 User 的主键有关,但我不确定如何修复它,因为回溯中的所有行都在实际的 Python 文件中。谢谢你的帮助。

编辑:posts/migrations/0002_post_created_by.py 由请求添加:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('posts', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='created_by',
            field=models.ForeignKey(default=b'pk', to=settings.AUTH_USER_MODEL),
        ),
     ]

【问题讨论】:

  • 你能显示posts/migrations/0002_post_created_by.py吗?
  • 是自定义的用户模型还是默认的?
  • 这是默认用户。我在原帖中添加了posts/migrations/0002_post_created_by.py

标签: python django python-2.7 django-models


【解决方案1】:

您之前似乎已将默认设置为b'pk',进行了迁移,然后将默认设置更改为None,然后尝试运行迁移。

您需要删除旧的(未应用的)迁移并在设置了default=None 时创建一个新的迁移。

但请注意,default=None 无论如何都是这种情况,None 默认值不适用于 null=False(此处隐式设置)。

【讨论】:

    【解决方案2】:
    field=models.ForeignKey(default=b'pk', to=settings.AUTH_USER_MODEL),
    

    这是ForeignKey 的基础IntegerField 的无效默认值。您必须将其更改为有效的整数值。

    【讨论】:

      猜你喜欢
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 2015-04-23
      • 2019-03-16
      • 2019-10-29
      • 2016-10-17
      相关资源
      最近更新 更多