外键默认值的ValueError [英] ValueError on foreignkey default value
问题描述
我删除了我的 db.sqlite3
和迁移文件夹中的文件,除了 __ init __.py
之外,但包括了 __ pycache __
文件夹.
I deleted my db.sqlite3
and the file on my migration folder except __init__.py
but including the __pycache__
folder.
我的应用程序的模型是一个文件夹中的多个文件,该文件夹中有一个带有某些导入的 __ init __.py
文件和一个 __ all __
变量(我不知道这有没有关系).
My app's models are multiple files in a folder with an __init__.py
file with some import and an __all__
variable (I don't know if this matter).
在提示符下,我给出了第一个迁移命令,它看起来很有效(迁移会话,管理,身份验证,注册,内容类型.没有我的应用程序核心").
From the prompt I give the first migrate command and it look to work (migrating sessions, admin, auth, registration, contenttypes. There isn't my app 'core').
因此,我给出了第一个makemigrations命令,然后再次给出了migration命令.这次它给出了一个错误:
So i give the first makemigrations command and again the migrate command. This time it give an error:
ValueError: invalid literal for int() with base 10: 'basic'
在模型中,有一个ForeignKey字段,其默认值="basic".basic将是相关类的对象,但实际上它不存在(在创建数据库之前,我将填充它).我认为这很正常.
In a model there's an ForeignKey fields with default='basic'. basic will be an object of the related class but actually it don't exist (before I create the database, then I will populate it). I think it's normal.
无论如何,我都更改了default = 1并可以正常工作(但是1不会成为对象,所以这是错误的!)
Anyway I change in default=1 and it works (but 1 will not be an object, so it's wrong!)
我的模型
:
Class Payment(models.Model):
name = models.CharField(max_length=32)
Class ProfileUser(models.Model):
payment = models.ForeignKey(Payment, blank=True, null=True,
default='basic', on_delete=models.PROTECT)
#etc
有一种使用基本"的方法吗?我宁愿不使用id = 1,因为我不确定基本的ID(请记住现在还不存在).
There is a way to use 'basic'? I prefer to not use id=1 because I'm not sure to know the id that basic will have (remember that now it don't exist yet).
在其他模型中,我有相似的情况,它们似乎起作用了...
Btw in other models I have alike situations and they appear to works...
整个错误:
(myvenv) c:\Python34\Scripts\possedimenti\sitopossedimenti>manage.py migrate
Operations to perform:
Apply all migrations: contenttypes, core, registration, admin, auth, sessions
Running migrations:
Rendering model states... DONE
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: 'basic'
谢谢
PS:我正在使用python 3.4 Django 1.9 Windows7
PS: I'm using python 3.4 Django 1.9 Windows7
推荐答案
在您的 Payment
模型中,您尚未显式指定主键.因此,django将为您创建一个.这将是一个整数自动递增字段.
In your Payment
model you have not explicitly designated a primary key. So django will create one for you. It will be an integer auto increment field.
Class Payment(models.Model):
name = models.CharField(max_length=32)
现在,当您将 Payment
定义为 ProfileUser
的外键时,django认为应该将其作为 Payment
中的主键字段用作参考.
Now when you define Payment
as a foreign key for ProfileUser
, django thinks that it should be the primary key field in Payment
that ought to be used as the reference.
Class ProfileUser(models.Model):
payment = models.ForeignKey(Payment, blank=True, null=True,
default='basic', on_delete=models.PROTECT)
#etc
为了强制执行此引用,付款字段必须为整数.而且您不能在整数字段中插入基本".
In order to enforce this reference, the payment field needs to be an integer. And you cannot insert 'basic' into an integer field.
一种解决方案是使用 ForeignKey.to_field 选项明确引用Payment类中的其他字段.
One solution is to use the ForeignKey.to_field option to explicitly refer to a different field in the Payment class.
ForeignKey.to_field¶与相关对象有关的字段是.默认情况下,Django使用相关对象的主键.
ForeignKey.to_field¶ The field on the related object that the relation is to. By default, Django uses the primary key of the related object.
这样您的班级就会变成
Class ProfileUser(models.Model):
payment = models.ForeignKey(Payment, blank=True, null=True,
default='basic', on_delete=models.PROTECT,
to_field = 'name' )
#etc
现在,此处的付款字段变为CharField.请注意,在大多数情况下,使用整数字段作为外键会更有效.
Now the payment field here becomes a CharField. Note that it would be more efficient to use an integer field as a foreign key in most cases.
另一种可能性是,您可以定义 Payment
中的 name
字段作为主键.
Another possibility is for you to define the name
field in Payment
as it's primary key.
这篇关于外键默认值的ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!