3

Im trying to use the datatables jQuery plugin with date/datetime fields. and when you make the query that contains those field generates a MySQL Warning when you try to use __contains in the queryset. I've detected that if you try to make queries over Year, Month and Day the warning doesn't appears.

There's another way to search in a date/datetime field without raise that warning or how could I make a query over a year/month/day in a date/datetime field.

Here is an example:

Model.objects.filter(Date__contains="2014")

Which gives this warning:

Warning: Incorrect date value: '%2014%' for column 'Fecha' at row 1

I'd like to make a query like this:

SELECT * FROM Model WHERE YEAR(Date) LIKE "%4%" OR MONTH(Date) LIKE "%4%" OR DAY(Date) LIKE "%4%";
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
MikeVelazco
  • 885
  • 2
  • 13
  • 30

2 Answers2

2

The best solution that I´ve found is searching by a Regex

Model.objects.filter(Date__regex="4")
MikeVelazco
  • 885
  • 2
  • 13
  • 30
0
Model.objects.filter(field_name__year=2014)
  • 1
    if you want to search for a number in any position of the date as it were a string, this doesn´t work. This only filter for a year given. – MikeVelazco May 12 '14 at 14:22