I use the example in the doc:
release_year__gt = django_filters.NumberFilter(name='release_date', lookup_expr='year__gt')
but i got the error:
TypeError: __init__() got an unexpected keyword argument 'name'
when i lookup the code, i found the 'name' has changed to 'field_name', the doc is old, right??
Also I use the code:
class MyFilter(django_filters.FilterSet):
class Meta:
model = FileEvent
fields = ['file_name', ]
above is ok, but when i use the code:
class MyFilter(django_filters.FilterSet):
class Meta:
model = FileEvent
fields = {'file_name' : ['icontains'] }
It is not work. the quey sql has no statement like %somevalue%
, why?
Thanks,
Kris
Hi @zhangqunshi, it sounds like you're using the 2.0.0.dev1 pre release. You are correct, name
has become field_name
. The docs have not been updated to reflect this (which is part of the reason why it's a pre release).
It's not easy to debug FileEvent
without seeing the actual model. What happens when you execute the following query?
FileEvent.objects.filter(file_name__icontains='foo')
FileEvent is simple, only has report_id, file_name and event_time. when i use:
FileEvent.objects.filter(file_name__icontains='foo')
It will become the sql:
select * from file_event where file_name like '%foo%'
For the code:
class MyFilter(django_filters.FilterSet):
class Meta:
model = FileEvent
fields = ['file_name', ]
it will become the sql:
select * from file_event where file_name ='foo'
For the code:
class MyFilter(django_filters.FilterSet):
class Meta:
model = FileEvent
fields = {'file_name' : ['icontains'] }
it will become the sql:
select * from file_event
BTW the code to get queryset is:
filter = MyFilter(request.GET,
queryset=FileEvent.objects.values().filter(report_id=1).order_by(
'-event_time'))
print(filter.qs)
And I cannot find any help from doc, that how to get result from filter in a common view, so I lookup the code, and find the field 'qs'in BaseFilterSet, and it works.
Now i change the code to:
class MyFilter(django_filters.FilterSet):
file_name = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = FileEvent
fields = ['file_name',]
and it works, the sql is like '%foo%'
@zhangqunshi, my guess would be that you're filtering with an incorrect querystring. It should be:
/file-events?file_name__icontains=foo
You are right, i use the url: /file-events?file_name=foo
Because i think the code is already define the file_name field should use the icontains.
Yep. It's counterintuitive at first, but the change is necessary because you can use the dictionary syntax to define multiple filters. For example,
class MyFilter(django_filters.FilterSet):
class Meta:
model = FileEvent
fields = {'file_name': ['icontains', 'istartswith', 'iendswith',]}
This enables you to run queries like /file-events?file_name__istarswith=foo&file_name__iendswith=log
ok, i got it, and the code ['icontains', 'istartswith', 'iendswith',]
is just a supported query type list. If the query not use any item of the list, django-filter will ignore it.
Thanks a lot :-)
Most helpful comment
Hi @zhangqunshi, it sounds like you're using the 2.0.0.dev1 pre release. You are correct,
name
has becomefield_name
. The docs have not been updated to reflect this (which is part of the reason why it's a pre release).It's not easy to debug
FileEvent
without seeing the actual model. What happens when you execute the following query?