在#34; model"上应用makmigration时没有问题。但迁移命令抛出异常"错误" ValueError:int()的基数为10的无效文字:' null'。 当我对#34; emails_for_help"进行更改时,我收到此错误帮助领域 我尝试过fake_migration并迁移但没有用
模型
class Setup_user(models.Model):
organization=models.CharField(max_length=200,blank=False,default="")
email_id=models.EmailField(unique=True)
CEO = 'CEO'
GENERAL_MANAGER = 'GM'
JCHOICES = (
(CEO, 'CEO'),
(GENERAL_MANAGER, 'General Manager'),
)
Designation = models.CharField(max_length=5,choices=JCHOICES)
LEVEL1 = "L1"
LEVEL2 = "L2"
LCHOICES = (
(LEVEL1,"Level 1"),
(LEVEL2,"Level 2"),
)
job_level=models.CharField(max_length=2,choices=LCHOICES)
EMAIL_CHOICES = ((str(q.email), str(q.email)) for q in User.objects.all())
emails_for_help = MultiSelectField(choices=EMAIL_CHOICES,default="null",max_length=5000,help_text="select with whom you want to share knowledge")
def __str__(self):
return self.email_id
错误:
C:\Users\Ekatech7\PycharmProjects\baseproject>python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, pkm_app, sessions
Running migrations:
Applying pkm_app.0015_auto_20180417_1723...Traceback (most recent call
last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\core\management\__init__.py", line 371, in
execute_from_command_line
utility.execute()
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\core\management\commands\migrate.py", line 200, in handle
fake_initial=fake_initial,
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\migrations\executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
fake_initial=fake_initial)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\migrations\executor.py", line 147, in
_migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\migrations\executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\migrations\migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\migrations\operations\fields.py", line 84, in
database_forwards
field,
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\backends\sqlite3\schema.py", line 306, in add_field
self._remake_table(model, create_field=field)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site -
packages\django\db\backends\sqlite3\schema.py", line 178, in _remake_table
self.effective_default(create_field)
File "C:\Users\Ekatech7\PycharmProjects\baseproject\venv\lib\site-
packages\django\db\backends\base\schema.py", line 224, in effective_default
default = field.get_db_prep_save(default, self.connection)
文件&#34; C:\ Users \ Ekatech7 \ PycharmProjects \ baseproject \ venv \ lib \ site- 在get_db_prep_save中的packages \ django \ db \ models \ fields \ related.py&#34;,第936行 return self.target_field.get_db_prep_save(value,connection = connection) 文件&#34; C:\ Users \ Ekatech7 \ PycharmProjects \ baseproject \ venv \ lib \ site- 在get_db_prep_save中的packages \ django \ db \ models \ _字段_init __。py&#34;,第767行 return self.get_db_prep_value(value,connection = connection,prepared = False) 文件&#34; C:\ Users \ Ekatech7 \ PycharmProjects \ baseproject \ venv \ lib \ site- packages \ django \ db \ models \ fields__init __。py&#34;,第939行,in get_db_prep_value value = self.get_prep_value(value) 文件&#34; C:\ Users \ Ekatech7 \ PycharmProjects \ baseproject \ venv \ lib \ site- 在get_prep_value中的packages \ django \ db \ models \ _字段_init __。py&#34;,第947行 return int(value) ValueError:int()的基数为10的无效文字:&#39; null&#39;
答案 0 :(得分:0)
您的CHOICES
没有”null”
作为选项。如果要存储空值,则必须设置null=True
,这意味着当modelField
为blank=True
时,空值将存储为空。如果您只是希望表单在加载时不选择选项,请不要设置default
。错误正在寻找:
EMAIL_CHOICES = (
(“null”, “null”)
(str(q.email), str(q.email)
)
我认为你的for
循环也向后倾斜。我相信它应该是:
for q in User.objects.all():
(str(q.email), str(q.email)
答案 1 :(得分:0)
问题已解决。 我选择在上述模型中选择的方法是错误的错误。 它通过使用多个关系来解决“电子邮件帮助”列和带有查询集approch的模型。
class Set_User_Form(ModelForm):
emails_for_help= forms.ModelMultipleChoiceField(queryset=User.objects.all(),widget=forms.CheckboxSelectMultiple)
class Meta:
model=Setupuser
exclude=["email_id"]