Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以10为基数的int()的文本无效:';pk';Python/Django_Python_Django_Python 2.7_Django Models - Fatal编程技术网

以10为基数的int()的文本无效:';pk';Python/Django

以10为基数的int()的文本无效:';pk';Python/Django,python,django,python-2.7,django-models,Python,Django,Python 2.7,Django Models,我在Django做一个项目,刚刚在我的一个模型中添加了一个字段,保存了创建它的用户。我将其默认值设置为“无”。现在,每当我尝试迁移时,我都会收到一个值错误,上面写着“invalid literal for int(),以10为底:‘pk’”。我的模型是这样的: class SampleModel(models.Model): created_by = models.ForeignKey(User, default=None) # other fields here 下面是我在尝

我在Django做一个项目,刚刚在我的一个模型中添加了一个字段,保存了创建它的用户。我将其默认值设置为“无”。现在,每当我尝试迁移时,我都会收到一个值错误,上面写着“invalid literal for int(),以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'

这是
外键
的基础
整型字段
的无效默认值。必须将其更改为有效的整数值

看起来您之前已经将默认值设置为
b'pk'
,进行了迁移,然后将默认值更改为
None
,然后尝试运行迁移

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


但是请注意,
default=None
无论如何都是这样,并且
None
默认值对于
null=False
(此处隐式设置)无效。

您可以显示由.py创建的
posts/migrations/0002\u post\u吗?它是自定义用户模型还是默认用户?它是默认用户。我将.py创建的
posts/migrations/0002\u post\u添加到原始帖子中。
# -*- 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),
        ),
     ]
field=models.ForeignKey(default=b'pk', to=settings.AUTH_USER_MODEL),