ForeignKey默认中int()错误的无效文字 [英] Invalid literal for int() error in ForeignKey default
问题描述
我的 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')
在我的第一次迁移中(所以我不还没有确定区域,数据库还不存在),则会出现此错误:
In my first migration (so I don't have definite the regions yet and the database don't exist) it give this error:
ValueError:int()的无效文字以10为底:'north'
ValueError: invalid literal for int() with base 10: 'north'
当然我不想要一个整数,为什么要寻找一个整数?
以及为什么它也不会给出相同的方向错误(如果我删除default ='north',它会起作用)?
Of course I don't want an integer, why it look for one? And why it don't give the same error for orientation too (if I delete default='north' it works)?
这是完整的错误:
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应该是主键(例如
As noted in the comments, ForeginKeys are expected to be the primary keys (e.g. an integer), or an instance of the model.
但是我认为您在Django中使用ForeignKey默认值遇到了更深层次的问题。
However I believe you are running into a deeper issue with ForeignKey default values in Django.
因为在加载模型时该值必须可供Django使用。
Because that value needs to be available to Django when the model is loaded.
您将无法制作 Region
查询,获取对象的主键并将其用作默认外键,并且该值也需要用于迁移脚本,这与lambda函数不兼容您可以做饭以获得外键值。
You're not going to be able to make a Region
query, get the object's primary key and use that as the default foreign key, and that value will need to be available for the migration script too, which are incompatible with lambda functions that you may cook up to get the foreign key value.
如果您确实需要该外键的默认值,建议您使用带有 Region
对象,记住它是主键,然后将其硬编码为外键值
If you really need a default value for that foreign key, I suggest you use a fixture with the Region
object, remember it's primary key, then hardcode that into the foreign key value
例如像这样的装置
model: app.Region
pk: 1
fields:
name: 'North'
然后使用:
region = models.ForeignKey(Region, default=1)
这篇关于ForeignKey默认中int()错误的无效文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!