1

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?

tas
  • 393
  • 1
  • 4
  • 13

2 Answers2

3

You need to create a datetime objects from the string and then filter using that object. You will probably want to filter objects that have a date greater then or less then the datetime object. For this you can used filter(datefield__gt=datetime_obj) or filter(datefield__lt=datetime_obj)

Community
  • 1
  • 1
Timmy O'Mahony
  • 53,000
  • 18
  • 155
  • 177
  • It can be any string, most likely I won't be able to create a datetime object from it. What I want is when the user types "Fe" for example, in datatable there should be only records dated February. Same with numbers. – tas Mar 27 '12 at 17:01
  • 1
    That's going to be very difficult. Think of all the possible values they could be typing. "F" = February, First, Friday etc. You will either need to submit the string to the server for parsing before looking up the date, or use javascript to parse the string and then use ajax to look up the datetime object – Timmy O'Mahony Mar 27 '12 at 17:19
2

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:

  1. Write python to parse the strings and come up with date objects to match them. This may be quite difficult. You will then want to use Q objects from django.db.models to construct a complex OR query. This will likely be slow when it gets to the database on any reasonable number or records.
  2. Figure out what the SQL would look like to generate what you're trying to do. This isn't going to be easy IMO as there is so much variance and the underlying representation of dates in your database isn't going to be a string so you're really going to be in for it. This is essentially manually doing the same thing that #1 does, except manually. Once you've constructed the SQL run a raw query.
  3. Convert to using django-haystack to use a full text search engine. This is a lot of infrastructure and has a lot of potential negatives from having to rewrite your code to anticipate search engine results. Depending on the backend you choose to plug into haystack it may be smart enough to be able to understand the strings entered and correctly search...or you may have to manually generate a ton of string representations to be in your search index so that they can be properly queried. Overall this type of thing is what full text search engines excel at because they understand language. Databases do not, they understand data and they don't get to be fuzzy.

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.

John
  • 5,166
  • 27
  • 31
  • As I answered to comment above: I want to filter dates, whose text representation contains entered substring. – tas Mar 27 '12 at 17:03