Filter by Date Field/Foreign Key returns error

Issue #12 closed
David Wasylciw created an issue

Hi there,

When I include a date field as a searchable field I get a popup error returned stated that "Incorrect date value '(whateverinput)%' specified for field start_date"

As well, when including a foreign key value as a search/filter field an error is also returned stating that <foreignkey1>.value is not a recognized field of the model.

If the search function could automatically handle these data types it would be immensely more helpful.

Cheers

Comments (5)

  1. Bruno Lottero

    This is beacuse the qs filters expects foreignkeys this way

    foreignkey__value__statement
    

    As a workaround i changed the qs filters this way

    def filter_queryset(self, qs):
            """ If search['value'] is provided then filter all searchable columns using istartswith
            """
            if not self.pre_camel_case_notation:
                # get global search value
                search = self.request.GET.get('search[value]', None)
                col_data = self.extract_datatables_column_data()
                q = Q()
                for col_no, col in enumerate(col_data):
                    # apply global search to all searchable columns
                    if search and col['searchable']:
                        q |= Q(**{'{0}__istartswith'.format(self.columns[col_no].replace('.', '__')): search})
    
                    # column specific filter
                    if col['search.value']:
                        qs = qs.filter(**{'{0}__istartswith'.format(self.columns[col_no].replace('.', '__')): col['search.value']})
                qs = qs.filter(q)
            return qs
    

    I know...it doesn't look nice, but it works...sort of...sometimes, still trying to find out why some times i get

    DoesNotExist
    <ModelName> matching query does not exist.
    

    UPDATE It does work, the error im getting is a problem in my own data

    This fix is merged in last version

  2. Bruno Lottero

    About DateField warnings, i think you should handle date/datetime formating properly in frontend (maybe using UI datepicker)

    Django can accept something like:

    query = Model.objects.filter(start_date__istartswith='2015-')
    

    This is gonna give you the actual results you're looking, but the DB engine (MySQL in my case) raises a warning because "2015-" is not a valid datetime value!!!

  3. Log in to comment