django-admin-search icon indicating copy to clipboard operation
django-admin-search copied to clipboard

Issue in filtering the attributes with null value.

Open kunal-8 opened this issue 3 years ago • 1 comments
trafficstars

Hi, There are two issues:

  1. There is an issue in filtering the attributes with null value.
  2. Always need to include __contains in CharField.

Example:


#models.py
class Emp(models.Model):
    first_name = models.CharField(blank=False, null=False, max_length=100)
    last_name = models.CharField(blank=True, null=True, default=None, max_length=100)

#search form
class EmpSearchForm(forms.Form):
   
    first_name = forms.CharField(
        required=False,
        widget=forms.TextInput(
            attrs={
                'filter_method': '__contains',
            }
        )
    )
    
    last_name = forms.CharField(
          required=False
      )
  1. If we want to filter only by first_name. The filter does not return any result when the value of last_name is null.

  2. If we want to filter only by first_name, then the above search form does not return any result because last_name's filter_method is not set to __contains. The generated URL is:

    emp/?first_name=John&last_name=

    Setting last_name's filter_method to __contains will work fine.

    last_name = forms.CharField(
       required=False,
       widget=forms.TextInput(
           attrs={
               'filter_method': '__contains',
           }
       )
    )
    

suggestion:

  1. We can ignore the filter whose value is not passed or empty in the query parameter. e.g., for emp/?first_name=John&last_name= filter should work only on first_name as the last_name is empty so it should be ignored.

kunal-8 avatar Jun 14 '22 05:06 kunal-8

Hi @kunal-8 now i have little time, to maintain this library, i you have time, please create a PR.

I believe that it is better to change the method django_admin_search.utils.get_field_value_default. or set has_field_value to false, if value is empty...

shinneider avatar Aug 05 '22 05:08 shinneider

@shinneider Please check the pull request https://github.com/shinneider/django-admin-search/pull/49 which fixes this issue.

anuj-scanova avatar Apr 19 '23 15:04 anuj-scanova

@anuj-scanova PR accepted, please, test on v0.3.13, i created today on pypi

shinneider avatar Apr 24 '23 20:04 shinneider