Issue
Once I click the posts in django admin then this message pops up ValueError at /admin/blog/post/ invalid literal for int() with base 10: b'27th June' yes, I put wrong form of data in DateField. I wanted to remove this data. that's why I tried to get into posts in django admin. is there anyway to fix this problem?
models.py
class Post(models.Model):
flight_date = models.DateField(blank=False)
Solution
The problem is that in SQLite dates are simply stored as strings, hence you can store any invalid date as long as it is a string in your column. But the cursor that is used will try to convert this date into a string, and at this level you will get an error. Hence you cannot resolve to using the ORM to solve this problem and must use SQL queries to solve this.
You can use a raw query from Django's shell (python manage.py shell
) if you don't know how to open the database shell, the below snippet assumes that your app is named test_app
, you will need to replace that with your own apps name:
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'")
Answered By - Abdul Aziz Barkat
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.