I don't know if this is possible, but I think it would highly useful if it were.
Basically, I want to filter a
DateField as though it were a string (see the
StackOverflow post to understand why)
If there was an option on a Q filter like this:
Q(field__icontains="data", TypeCheck=False)
^^^^^^^^^^^^^^^^^^
(instructing Django to bypass type checking for this field)
it would then be possible to do datatype conversion on the database level, allowing code like this to work
from django.db.models import Lookup
from django.db.models.fields import DateField
@DateField.register_lookup
class DateTextFilter(Lookup):
lookup_name = 'dttxt'
def as_postgresql(self, compiler, connection):
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs = self.rhs.strftime("%b %d, %Y") # this would be replaced by a partial date. e.g., 'Mar 05'
return "to_char(%s,'Mon DD, YYYY') ~* '%s'", ([lhs,rhs])
Currently, this causes a ValidationError
ValidationError: [u"'Mar 09' value has an invalid date format.
It must be in YYYY-MM-DD format."]