ForeignKey 默认值中 int() 错误的无效文字

Posted

技术标签:

【中文标题】ForeignKey 默认值中 int() 错误的无效文字【英文标题】:Invalid literal for int() error in ForeignKey default 【发布时间】:2016-04-24 00:46:17 【问题描述】:

我的models.py

class orientation_type(models.Model):
    nome = models.CharField(max_length=16, unique=True)
    def __str__(self):
        return self.nome

class region(models.Model):
    nome = models.CharField(max_length=16, unique=True)
    def __str__(self):
        return self.nome

class province(models.Model):
    city = models.OneToOneField(community, blank= True, null=True)
    orientation = models.ForeignKey(orientation_type, default='generic')
    region = models.ForeignKey(region, default='north')

在我的第一次迁移中(所以我还没有确定的区域并且数据库不存在)它给出了这个错误:

ValueError: int() 以 10 为底的无效文字:'north'

我当然不想要一个整数,为什么要找一个呢? 为什么它也没有给出相同的方向错误(如果我删除 default='north' 它可以工作)?

这是完整的错误:

manage.py migrate

Operations to perform:
  Apply all migrations: admin, sessions, core, auth, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying core.0001_initial...Traceback (most recent call last):
  File "C:\Python34\Scripts\possedimenti\sitopossedimenti\manage.py", line 10, i
n <module>
    execute_from_command_line(sys.argv)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\operations\fields.py", line 62, in database_forwards
    field,
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\sqlite3\schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\sqlite3\schema.py", line 103, in _remake_table
    self.effective_default(field)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\base\schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\related.py", line 912, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 968, in get_db_prep_value
    value = self.get_prep_value(value)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'north'

【问题讨论】:

外键在内部是整数(特别是被指向的行的 ID)。 "north" 不是整数。 【参考方案1】:

如 cmets 中所述,ForeginKeys 应该是主键(例如整数)或模型的实例。

但是,我相信您在 Django 中遇到了 ForeignKey 默认值的更深层次的问题。

因为在加载模型时该值需要可供 Django 使用。

您将无法进行Region 查询,获取对象的主键并将其用作默认外键,并且该值也需要可用于迁移脚本,这是不兼容的使用您可以编写的 lambda 函数来获取外键值。

More on the issues on this topic in this question.

如果您确实需要该外键的默认值,我建议您使用带有 Region 对象的夹具,记住它是主键,然后将其硬编码到外键值中

例如像这样的夹具

model: app.Region
pk: 1
fields:
    name: 'North'

然后使用:

region = models.ForeignKey(Region, default=1)

【讨论】:

以上是关于ForeignKey 默认值中 int() 错误的无效文字的主要内容,如果未能解决你的问题,请参考以下文章

Docker:在分配给网络的默认值中找不到可用的、非重叠的 IPv4 地址池

默认值中的kotlin空指针异常

Django 如何使 ForeignKey 使用默认值?

在用户默认值中保存包含编码为数据的敏感数据的对象

当来自模型时如何将数据保存在用户默认值中

在管理站点中更改 ForeignKey 的默认值