我无法进入django管理的帖子



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

models.py
class Post(models.Model):   
flight_date = models.DateField(blank=False)

我去了django-shell,放了这个代码,但得到了这个错误;

>>> from django.db import connection
>>> with connection.cursor() as cursor:
... cursor.execute("UPDATE blog_post SET flight_date='2021-05-27' WHERE flight_date='27th June'")
File "<console>", line 2
cursor.execute("UPDATE blog_post SET flight_date='2021-05-27' WHERE flight_date='27th June'")
^
IndentationError: expected an indented block

我不知道这个代码的问题是什么

问题是,在SQLite中,日期只是以字符串的形式存储,因此您可以存储任何无效的日期,只要它是列中的字符串。但是使用的光标会尝试将此日期转换为字符串,在这个级别上,您会得到一个错误。因此,您无法解决使用ORM来解决此问题,必须使用SQL查询来解决该问题。

如果你不知道如何打开数据库外壳,你可以使用Django的外壳(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'")

请通过命令进入Django shell

python manage.py shell

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

最新更新