Search code examples
pythondjangodjango-crispy-formsdjango-tables2

Django FieldError: Unsupported lookup 'kategorie' for IntegerField or join on the field not permitted


I have a Django Table with Crispy Filter and I would like to filter in Data table based on Category. But I am getting FieldError.

I tried to define my filter field by this way in filters.py:

kategorie = django_filters.CharFilter(label="Kategorie", field_name="ucet__cislo__kategorie__jmeno", lookup_expr='icontains')

And I have following structure of models in models.py:

class Data(models.Model):
    ucet = models.ForeignKey(Ucty, on_delete=models.CASCADE, null=True, blank=True)

class Ucty(models.Model):
    cislo = models.IntegerField("Účet", blank=True, null=True)

class Mustek(models.Model):
    ucet = models.ForeignKey(Ucty, on_delete=models.CASCADE)
    kategorie = models.ForeignKey(Kategorie, on_delete=models.CASCADE)

class Kategorie(models.Model):
    jmeno = models.CharField("Kategorie", max_length=20, blank=True, null=True)


Any idea how to correct field_name definition in filter?


Solution

  • Since cislo is an IntegerField, you can not join on this. you probably want to join on Mustek in reverse however, so:

    kategorie = django_filters.CharFilter(
        label='Kategorie',
        field_name='ucet__mustek__kategorie__jmeno',
        lookup_expr='icontains',
    )