我无法进入 django admin 中的帖子

问题描述 投票:0回答:2

一旦我点击 django admin 中的帖子,就会弹出此消息 ValueError at /admin/blog/post/ 以 10 为基数的 int() 的文字无效:b'27th June' 是的,我在 DateField 中放置了错误的数据形式。我想删除这些数据。这就是为什么我尝试进入 django admin 中的帖子。有办法解决这个问题吗?

models.py
class Post(models.Model):   

    flight_date = models.DateField(blank=False)

python django django-models django-admin
2个回答
1
投票

问题在于,在 SQLite 中,日期只是存储为字符串,因此您可以存储任何无效日期,只要它是列中的字符串即可。但是使用的游标会尝试将此日期转换为字符串,在这个级别您将收到错误。因此你不能决定使用 ORM 来解决这个问题,而必须使用 SQL 查询来解决这个问题。

如果您不知道如何打开数据库 shell,您可以使用 Django shell 中的原始查询 (

python manage.py shell
),下面的代码片段假设您的应用程序名为
test_app
,您需要将其替换为您自己的应用程序名称:

from django.db import connection


with connection.cursor() as cursor:
    cursor.execute("UPDATE test_app_post SET flight_date='2021-05-27' WHERE flight_date='27th June'")

0
投票

请通过此命令进入 Django shell

python管理.py shell

然后从模型文件导入模型并触发更新查询

© www.soinside.com 2019 - 2024. All rights reserved.