Why not do this in the database? Create a view with an extra column that is
called "field_dttxt" that is to_char(field, 'Mon DD, YYYY'). Then add this
as a field to Django and let it use the normal Q lookups.

On Mon, Mar 28, 2016 at 5:13 PM, Michael E <[email protected]> wrote:

> 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
> <http://stackoverflow.com/questions/36191609/how-can-i-filter-django-datefield-using-a-string>
>  to
> understand why)
>
> If there was an option on a Q filter like this:
>
>  Q(field__icontains="data", *T**ypeCheck=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 Lookupfrom django.db.models.fields import 
> DateField
> @DateField.register_lookupclass 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."]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/f38f1e24-df0e-422f-a1c3-5105911a8ece%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/f38f1e24-df0e-422f-a1c3-5105911a8ece%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAD4ANxXBxk-4OvuDn%2BVddo0r9no1x%2Bd0rLPqzHjg1yvChw4BiA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to