Using Django-Filter to filter data from two related Models

Good day all,

I have a challenge in my project. I am using Django-Filter to generate report from my model, It works very fine using one Model and I can get all my report using the filter generated.

But when I tried to include the related table to pick some fields from the second table, I got the error Meta.fields must not contain non-model field names:

I used a sample codes I got online

Model.py

from django.db import models 

class Customer(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    order_date = models.DateField()
    total_price = models.DecimalField(max_digits=10, decimal_places=2)

Filter.py

import django_filters

class OrderFilter(django_filters.FilterSet):
    customer_name = django_filters.CharFilter(field_name='customer__name', lookup_expr='icontains')

    class Meta:
        model = Order
        fields = ['customer_name', 'order_date']

But still generating the error.

Any help will be highly appreciated.
Thanks.

This is stated on the documentation of the filterset.

Note that it is not necessary to include declared filters in a fields list - doing so will only affect the order in which fields appear on a FilterSet’s form. Including declarative aliases in a fields dict will raise an error.

So the correct declarion would be:

import django_filters

class OrderFilter(django_filters.FilterSet):
    customer_name = django_filters.CharFilter(field_name='customer__name', lookup_expr='icontains')

    class Meta:
        model = Order
        fields = ['order_date']  # customer_name not included on fields

Thank you very much. I reverted back to the configuration that worked.

parsed = urlparse(oReport.filter_params)
captured_value = parse_qs(parsed.query)
filter_params = {key: value[0] for key, value in captured_value.items()}
#queryset=Initiative.objects.filter(**filter_params)
queryset2=InitiativeImpact.objects.select_related('initiativeId').filter(**filter_params)

I then joined the tables in my views.py,

InitiativeImpact.objects.select_related('initiativeId').filter(**filter_params)

I was able to select all the fields and even the properties on the other table.

Thanks for the suggestion.

But, why can’t be a workaround by the django-filter project to fix this issue?

Maybe it should be something like

fields = ['customer__name', 'order_date']

? I don’t use Django-filters, but that’s how iommi works for the corresponding thing.

Note the double underscores.

It must have been fixed in iommi. Then django-filters should do the same.

Thank you very much.