gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:11:34 24 4
gpt4 key购买 nike

我的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: invalid literal for int() with base 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'

最佳答案

正如评论中所指出的,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)

关于python - ForeignKey 默认值中 int() 错误的文字无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com