Datatable on the page can be filtered by user input. Entered string is received in ajax query and I’m trying to filter objects by this string. String fields are filtered fine, but when I try to filter over a date:
querySet.filter(datefield__icontains=searchString)
It leads to MySQL exception:
Warning: Incorrect datetime value: ‘%%’ for column ” at row 1
Is there a way to filter over date fields using a string?
I’m not sure what you would expect icontains to do when filtering over date data, so you may want to rethink that or clarify what your are expecting.
Overall you are trying to query based on the datefield. You have 2 choices. You either need to convert your sting into a date (or datetime) object, or you need to format your string as “yyyy-mm-dd”.
— edit —
Since it looks like you’re really trying to use strings to search for dates here is some sort of clarification.
What you’re looking to do is going to be beyond hard to do. I’d go with borderline impossible. Django is translating your queries into SQL and querying your database. Just because the ORM is there doesn’t make it able to do things not possible in SQL (in fact the ORM greatly limits what you can do). However, you have several choices:
Qobjects fromdjango.db.modelsto construct a complex OR query. This will likely be slow when it gets to the database on any reasonable number or records.rawquery.More or less what you’re asking for is quite hard. The most viable option in my opinion is to convert to using haystack. I can’t emphasize enough how many drawbacks there are to that. You’re dealing with search engine results instead of model instances. In some circumstances that isn’t an issue, but depending on the requirements, that could be quite painful.