valueerror:字段' id'期望有一个数字,但得到了shafquetnaghmi'

发布于 2025-02-10 19:56:26 字数 2913 浏览 1 评论 0原文

添加此模型

后,当我运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2025-02-17 19:56:26

您已经用代码删除了它,但是在迁移中仍然应用。

建议的修复:
您必须删除所有迁移 - SocialApp.0006_Remove_instantMessage_sender_InstantMessage_sender及以后的迁移(0007 _*。py,如果有)。然后删除数据库并运行makemigrations再次迁移

您还可以编辑SocialApp.0006_Remove_instantMessage_Sender_InstantMessage_sender和delete default ='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 run makemigrations and migrate again.

You can also edit socialapp.0006_remove_instantmessage_sender_instantmessage_sender and delete default='shafquetnaghmi' from that model there. It also needs database drop probably.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文