valueerror:字段' id'期望有一个数字,但得到了shafquetnaghmi'
添加此模型
后,当我运行python manage.py迁移此问题时,我在发件人中添加了default ='shafquetnaghmi',但我将其删除,但它仍然无法正常工作。
class instantmessage(models.Model):
sender=models.ForeignKey(User,related_name='sender', on_delete=models.CASCADE,blank=True,null=True)
receiver=models.ManyToManyField(User,related_name='receiver')
message=models.TextField(blank=True)
def __str__(self):
return f'{self.message}'
执行操作:
Apply all migrations: admin, auth, contenttypes, sessions, social_django, socialapp
Running migrations:
Applying socialapp.0006_remove_instantmessage_sender_instantmessage_sender...Traceback (most recent call last):
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1822, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'shafquetnaghmi'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\SHAFQUET NAGHMI\socialnetwork\manage.py", line 22, in <module>
main()
File "C:\Users\SHAFQUET NAGHMI\socialnetwork\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
utility.execute()
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 417, in execute
output = self.handle(*args, **options)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 253, in hanp_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 2461, in get_db_prep_value
value = self.get_prep_value(value) File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1824, in get_prep_value
raise e.__class__(ValueError: Field 'id' expected a number but got 'shafquetnaghmi'.
models.py
After adding this model,when i run python manage.py migrate this problem arises,there i added default='shafquetnaghmi' in sender but i removed it ,still it is not working.
class instantmessage(models.Model):
sender=models.ForeignKey(User,related_name='sender', on_delete=models.CASCADE,blank=True,null=True)
receiver=models.ManyToManyField(User,related_name='receiver')
message=models.TextField(blank=True)
def __str__(self):
return f'{self.message}'
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, social_django, socialapp
Running migrations:
Applying socialapp.0006_remove_instantmessage_sender_instantmessage_sender...Traceback (most recent call last):
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1822, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'shafquetnaghmi'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\SHAFQUET NAGHMI\socialnetwork\manage.py", line 22, in <module>
main()
File "C:\Users\SHAFQUET NAGHMI\socialnetwork\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
utility.execute()
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 417, in execute
output = self.handle(*args, **options)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 90, in wrapped
res = handle_func(*args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 253, in hanp_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 2461, in get_db_prep_value
value = self.get_prep_value(value) File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\fields\__init__.py", line 1824, in get_prep_value
raise e.__class__(ValueError: Field 'id' expected a number but got 'shafquetnaghmi'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经用代码删除了它,但是在迁移中仍然应用。
建议的修复:
您必须删除所有迁移 -
SocialApp.0006_Remove_instantMessage_sender_InstantMessage_sender
及以后的迁移(0007 _*。py,如果有)。然后删除数据库并运行makemigrations
和再次迁移
。您还可以编辑
SocialApp.0006_Remove_instantMessage_Sender_InstantMessage_sender
和deletedefault ='shafquetnaghmi'从那里的模型。它也可能需要数据库下降。
You have deleted it in code, but in migrations it is still applied.
Suggested fix:
You have to delete all migrations -
socialapp.0006_remove_instantmessage_sender_instantmessage_sender
and later ones (0007_*.py and on, if you have them). Then delete database and runmakemigrations
andmigrate
again.You can also edit
socialapp.0006_remove_instantmessage_sender_instantmessage_sender
and deletedefault='shafquetnaghmi'
from that model there. It also needs database drop probably.