Django-filter: FilterSet resolved field 'image' with 'exact' lookup to an unrecognized field type ImageField...

Created on 24 Jul 2017  ·  11Comments  ·  Source: carltongibson/django-filter

Hi there!
I know this issue has been addressed here already, but I just can't find a proper solution.

I have a model with a Store class that contains various information about different stores and includes also a field with image = models.ImageField to display when querying for it.

I tried everything from the docs, but it seems to be ignoring my exlude and also the fields I'm filtering for

class StoreFilter(django_filters.FilterSet):
    class Meta:
        model = Store
        exclude = ['image']
        fields = {
            'name': ['exact', 'contains'],
            'created': ['exact', 'year__gt'],
        }

This is what I'm trying to filter for and it keeps throwing me that AssertionError, any idea what I might be missing? As soon as I comment the image line in my models.py it works (but it shows all fields, not only name and created which I was filtering for. Any pointers would be appreciated.

Best regards

Most helpful comment

Hi @msbt - I'd wager that some information has been left out here. I ran a quick test, and the mentioned AssertionError was not raised. As you can see below, no filter for the image fields was created.

```python

models

from django.db import models

class Store(models.Model):
name = models.CharField(max_length=80)
created = models.DateField()
image = models.ImageField()

filters

import django_filters

class StoreFilter(django_filters.FilterSet):
class Meta:
model = Store
exclude = ['image']
fields = {
'name': ['exact', 'contains'],
'created': ['exact', 'year__gt'],
}

console output

In [8]: StoreFilter.base_filters
Out[8]:
OrderedDict([('name', ),
('name__contains', ),
('created', ),
('created__year__gt', )])
````

Could you provide the full model & filter code? If not, a distilled example that demonstrates the issue would be helpful.

All 11 comments

Hi @msbt - I'd wager that some information has been left out here. I ran a quick test, and the mentioned AssertionError was not raised. As you can see below, no filter for the image fields was created.

```python

models

from django.db import models

class Store(models.Model):
name = models.CharField(max_length=80)
created = models.DateField()
image = models.ImageField()

filters

import django_filters

class StoreFilter(django_filters.FilterSet):
class Meta:
model = Store
exclude = ['image']
fields = {
'name': ['exact', 'contains'],
'created': ['exact', 'year__gt'],
}

console output

In [8]: StoreFilter.base_filters
Out[8]:
OrderedDict([('name', ),
('name__contains', ),
('created', ),
('created__year__gt', )])
````

Could you provide the full model & filter code? If not, a distilled example that demonstrates the issue would be helpful.

Hey, thanks for getting back to me! Give me a bit more time to figure out what I might have done wrong, would be embarassing if it was a rookie mistake ;)

Hi @msbt - did you ever figure out what was going on, or can this be closed?

I don't have time right now, so lets close it up and revisit when I'm trying again and get stuck ;)

@msbt Any idea about this problem? I have the same one

@georgezouq afraid not, gave up the project for different reasons and never found a solution

Traceback please. 😀

Internal Server Error: /stadiums/
Traceback (most recent call last):
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\*\PycharmProjects\pina_colada\venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\*\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\views.py", line 77, in get
    filterset_class = self.get_filterset_class()
  File "C:\Users\*\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\views.py", line 35, in get_filterset_class
    return filterset_factory(model=self.model, fields=self.filterset_fields)
  File "C:\Users\*\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\filterset.py", line 459, in filterset_factory
    (FilterSet,), {'Meta': meta})
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\filterset.py", line 71, in __new__
    new_class.base_filters = new_class.get_filters()
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\filterset.py", line 345, in get_filters
    filters[filter_name] = cls.filter_for_field(field, field_name, lookup_expr)
  File "C:\Users\default_user\PycharmProjects\pina_colada\venv\lib\site-packages\django_filters\filterset.py", line 377, in filter_for_field
    ) % (cls.__name__, field_name, lookup_expr, field.__class__.__name__)
AssertionError: StadiumFilterSet resolved field 'graphic' with 'exact' lookup to an unrecognized field type FileField. Try adding an override to 'Meta.filter_overrides'. See: https://django-filter.readthedocs.io/en/master/ref/filterset.html#customise-filter-generation-with-filter-overrides
[13/Dec/2018 08:23:58] "GET /stadiums/?page=1 HTTP/1.1" 500 108150
class StadiumFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(lookup_expr='iexact')

    class Meta:
        model = Stadium
        exclude = ['graphic']
        filter_overrides = {
            models.FileField: {
                'filter_class': django_filters,
                'extra': lambda f: {
                    'lookup_expr': 'icontains',
                },
            },
        }

@carltongibson may you have a look

it was ignoring my overrides since I was using a generic view. this has temporarily solved my problem but my client is bothering me in the specificity of his filters lol. Could you look into this

urlpatterns = [
    path('', views.StadiumFilterView.as_view(model=Stadium,
                                             filterset_fields=['name', 'country']), name="stadium_index"),
]

fields, exclude, filter_overrides in Meta doesn't solve this AssertionError error, when i use FilterView + filter_class = MyFilterSet , i have to set filter_fields under view to exclude fields,
otherwise def filter_for_field(cls, f, name, lookup_expr='exact') will run through all fields

Was this page helpful?
0 / 5 - 0 ratings