How to get field from different model to filter in

is there some way how get field from different model to my filter which is used in my template? I am trying to filter by category_name from Category model in Warehouse model which is connected with Category Model by Product model.

When I try in shell, relation is working:


But when I use similar code in, I get blank field instead of field for category (other fields from filter are working, so filter is OK):

class WarehouseDataFilter(django_filters.FilterSet):
    product = django_filters.CharFilter(label="Product", field_name="product__name", lookup_expr='icontains')
    category = django_filters.CharFilter(label="Kategorie", field_name="product__category__category_name", lookup_expr="icontains" ),
    pyear = django_filters.NumberFilter(label="Rok výroby", field_name="product__pyear")

    class Meta:
        model = Warehouse
        fields = ("product", "pieces")
        attrs={"a": {"class": "orange"}, "thead": {"class": "orange"}}
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.form.helper = WarehouseDataFilterFormHelper()

class Warehouse(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, blank=True)
    pieces = models.IntegerField("Quantity", null=True, blank=True)
    updated_on = models.DateTimeField(auto_now=True, null=True)

class Product(models.Model):
    name = models.CharField("Product", max_length=150)
    pyear = models.IntegerField("Rok Výroby", null=True, blank=True)
    drop = models.IntegerField("Drop", null=True, blank=True)
    productgroup = models.ForeignKey(ProductGroup, on_delete=models.CASCADE, null=True, blank=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, null=True, blank=True)

class Category(models.Model):
    category_name = models.CharField("Category", max_length=100, blank=True, null=True)

Back to Top