How to allow null or blank value for a range filter field.

523 views
Skip to first unread message

Akira Inamori

unread,
Jun 19, 2017, 9:54:21 PM6/19/17
to django-filter
Hi, I have a problem for using range fields.
I'd like to use some filter forms that includes range fields.
An user can user any fields for filtering data.
If the user dont't fill out a filed, the blank field should not be used for filtering.

ex)
 When an user use a only "check no" field for filtering, the query shuld be the following.
 "Model.objects.filter(check_no="<input filtering data>")

However once I create a range field on filter form, the range filed doesn't allow to blank or empty value.

Could you tell me how to allow empty value for a range field please ?



--------------------------
- models.py

class model_table(models.Model):
    check_no = models.AutoField("check no", primary_key=True)
    check_date = models.DateField("check date", default=timezone.now)
    pay_status = models.ForeignKey('pay_status')
    tcl_date = models.DateField("tcl Date", default=timezone.now)
   
- filters.py

class filter_Filter(django_filters.FilterSet):
    class Meta:
        model = Trans_table
        fields = {
            'check_no': ['exact'],
            'check_date': ['range'],
            'pay_status': ['exact'],
            'tcl_date': ['range'],
        }
        order_by = ['check_no'

- views.py

class Upload_bank_data_csvFilterView(PagedFilteredTableView):
    model = model_table
    template_name = "filter.html"
    filter_class = filter_Filter
   
Best regards,

Akira


problem.png

Carlton Gibson

unread,
Jun 20, 2017, 2:58:36 AM6/20/17
to django-filter
HI Akira. 

I'd need to dig into exactly what's going on there. It's possible there's a bug in handling the `check_date=` empty value from the form. 

However, I'd advise you to be declaring your filters explicitly, rather than using the dictionary `fields` syntax, and then you could use a `DateFromToRangeFilter`:

    ...
    check_date = DateFromToRangeFilter()
    ...


Kind regards, 
Carlton

Akira Inamori

unread,
Jun 20, 2017, 11:50:03 AM6/20/17
to django-filter
Hi Carlton,

Thank you for your great help.
I added the following into filters.py because "check_date =" works for check_date"exact".
Then it works !

check_date__range = django_filters.DateFromToRangeFilter()

However, I got a new problem."Unsupported lookup 'range' for DateField or join on the field not permitted."
So I cannot use the "DateFromToRangeFilter()".

It seems that Django had the same problem but it already fixed.
I need to read the report and django-filter documents.

https://code.djangoproject.com/ticket/24837

Anyway, my enviroment is the followings.
 - CentOS 7
 - python 3.4 -- from epel packages
 - Django 1.11  -- pip3 installed
 - django-filter -- latest ver. (from pip3 install django-filter)

Best regards,

--
Akira

2017年6月19日月曜日 23時58分36秒 UTC-7 Carlton Gibson:
Reply all
Reply to author
Forward
0 new messages