philipn / django-rest-framework-filters

Better filtering for Django REST Framework
Other
847 stars 131 forks source link

Invalid filter for properties of model of RelatedMultipleFilter #349

Open petrosschilling opened 4 years ago

petrosschilling commented 4 years ago

Version: 1.0.0.dev2 Python version: 3.6.11

Other dependencies: Django==3.0 django-cors-headers==3.5.0 django-filter==2.4.0 django-hashid-field==3.1.3 djangorestframework==3.11.1 djangorestframework-filters==1.0.0.dev2 djangorestframework-jsonapi==3.2.0 djangorestframework-simplejwt==4.4.0 dry-rest-permissions==0.1.10

I know I'm using a dev version but i'm putting the issue here for the record.

I have the following structure:

class IndividualFilterSet(FilterSet):
    class Meta:
        model = Individual
        fields = {
            "first_name": ["exact", "icontains"],
            "last_name": ["exact", "icontains"],
            "mobile_phone": ["exact", "icontains"],
        }

class ClientAccountFilterSet(FilterSet):
    individuals = RelatedMultipleFilter(IndividualFilterSet, queryset=Individual.objects.all())

    class Meta:
        model = ClientAccount
        fields = {
            "is_lead": ["exact"],
            "user_reference": ["exact", "icontains"],
        }

But when making a get request on /client_accounts/?filter[individuals.first_name]=Tes I get the following error:

{
    "errors": [
        {
            "detail": "invalid filter[individuals__first_name]",
            "status": "400",
            "source": {
                "pointer": "/data"
            },
            "code": "invalid"
        }
    ]
}

If I add the field to individuals__first_name to the client ClientAccountFilterSet Meta fields like the following. I passes through the validation.

class ClientAccountFilterSet(FilterSet):
    individuals = RelatedMultipleFilter(IndividualFilterSet, queryset=Individual.objects.all())

    class Meta:
        model = ClientAccount
        fields = {
            "is_lead": ["exact"],
            "user_reference": ["exact", "icontains"],
            "individuals__first_name": ["exact", "icontains"],
        }

As far as I understand when using related filter I should not have to specify all the properties within the related class in the filter because that is already specified in the IndividualFilterSet. Like the case of the Individual.first_name.

Any help on this is much appreciated.

petrosschilling commented 4 years ago

The issue seems to be in the FilterSetMetaclass.expand_auto_filter() function which always return an empty OrderedDict().