philipn/django-rest-framework-filters

RelatedFilter 400s when Related Object not found by Id

ferrants opened this issue · 4 comments

Versions:

django-filter==2.0.0
djangorestframework-filters==1.0.0.dev0

If I filter by a related object and that related object is not found, I get a 400 error and an error like:

{
    'id': [{'manufacturer': 'Select a valid choice. That choice is not one of the available choices.', 'code': 'invalid_choice'}],
}

I have a a FilterSet defined like:

class ProductFilter(filters.FilterSet):
    manufacturer = filters.RelatedFilter('ManufacturerFilter', queryset=Manufacturer.objects.all(), lookups='__all__')  # `lookups` also accepts a list

    class Meta:
        model = Product

If I do a filter like:

/api/products/?manufacturer=100
/api/products/?manufacturer__in=100,101

# no error
/api/products/?manufacturer__name=something_that_doesnt_exist

This may also be a django-filter issue. Has anyone run into this and figured out how to get it to return a 200 with an empty list, like it did in previous versions.

Note, if the related id exists, this works as expected. The only issue is when the object is not found.

Also, suffixing __id works, but I'd like to not change the interface and maintain the previous behavior. Also, I think having the __id does a lookup on the manufacturer's object, I'd like to avoid multiple queries.

/api/products/?manufacturer__id=1 # object exists

Hi @ferrants. Sorry - I'm not following the description above. Can you provide more explicit cases that match requests/responses? e.g., "For this querysting, I get this response/error."

Thanks.

Also, if you have the time, creating a PR with an example test case would be appreciated. Alternatively (if you don't want to go through the hassle), you can post the code here.

Just realized - this was an issue with the django-filter 2.0 release and changes to strictness behavior. This should have recently been fixed bit the 2.1 release.