morlandi / django-ajax-datatable

A Django app which provides the integration of a Django project with the jQuery Javascript library DataTables.net
MIT License
204 stars 64 forks source link

Issue with Global and Column Search when using PositiveIntegerField. #88

Open kieriosity opened 2 years ago

kieriosity commented 2 years ago

I'm having an issue with the global search. When I attempt to search, I receive an "AttributeError: 'int' object has no attribute 'lower'" error. The error is obvious; you can't apply lower to int. I modified the code (below) on my local copy to cast all input variables to string and it worked, but I'm not sure if that will break something else or if it's band-aiding an issue with my code elsewhere.

The issue appears to be related to the report_year column in my code. It's a PostiveIntegerField. I have another PostiveIntegerField for the month, but it's using the string value from the list, so that is probably why it's not causing issues.

The column search is also not working unless I switch it to choices.

Model Code

This is looping and adding year values for every year since 1970 to the list.

    YEARCHOICES = []
    for r in range(1970, (datetime.datetime.now().year + 1)):
        YEARCHOICES.append((r, r))

This is the model field.

    report_year = models.PositiveIntegerField('report_year',
                                              choices=YEARCHOICES,
                                              default=datetime.datetime.now().year)

The column definition for the AjaxDatatableView {'name': 'report_year', 'visible': True, 'choices': True, 'title': 'Report Year', },

Code changes that work for global. I didn't modify the column search because choices worked.

Changed pattern.lower() to str(pattern).lower() and text.lower() to str(text).lower()

    def search_in_choices(self, pattern_list):
        if not self._allow_choices_lookup:
            return []
        # return [matching_value for key, matching_value in
        # six.iteritems(self._search_choices_lookup) if key.startswith(value)]
        values = []
        if type(pattern_list) != list:
            pattern_list = [pattern_list]
        for pattern in pattern_list:
            pattern = str(pattern).lower()
                # values = [key for (key, text) in self._choices_lookup.items() if text.lower().startswith(pattern)]
            values += [key for (key, text) in self._choices_lookup.items() if pattern in str(text).lower()]
        return values

Am I missing something obvious or doing something incorrectly?

morlandi commented 2 years ago

Thank you @kieriosity for report this with so much and useful details. I will try to reproduce it as soon as time allows