An idea for Q filter objects

126 views
Skip to first unread message

Michael E

unread,
Mar 28, 2016, 8:00:19 PM3/28/16
to Django developers (Contributions to Django itself)
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."]

Tim Graham

unread,
Mar 28, 2016, 8:25:52 PM3/28/16
to Django developers (Contributions to Django itself)
I think the issue would be fixed by tackling the ticket, "Get rid of field.get_db_prep_lookup()" [0]. If I understand correctly, the exception is coming from DateField.get_db_prep_value() which is trying to do the conversion to a datetime.

[0] https://code.djangoproject.com/ticket/22936

Stephen J. Butler

unread,
Mar 28, 2016, 10:07:22 PM3/28/16
to django-d...@googlegroups.com
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.

--
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 django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.

Shai Berger

unread,
Mar 30, 2016, 7:17:20 PM3/30/16
to django-d...@googlegroups.com
On Tuesday 29 March 2016 05:07:14 Stephen J. Butler wrote:
> 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.
>

Why would you need to create a view? Can't you use a Transform?

Stephen J. Butler

unread,
Mar 30, 2016, 8:17:45 PM3/30/16
to django-d...@googlegroups.com
I guess you could! I'm not as familiar with the Transforms API, but it looks like it would work too.
Reply all
Reply to author
Forward
0 new messages