carltongibson/django-filter

Upgrading 22.1 to 24.2 test fails for some queries (404 not found)

bertek41 opened this issue · 6 comments

I am not sure if this from django rest framework or this but its filter problem. Filters works in my local but fails in tests.

from rest_framework.test import APITestCase
class FiltersetTests(APITestCase):
...
response = self.client.get("", data={"category": 1}, HTTP_AUTHORIZATION="TEST")

this works fine but

response = self.client.get("", data={"brand": 1}, HTTP_AUTHORIZATION="TEST")

this fails and prints Not Found: /.
This is my filters:

    brand = filters.CharFilter(label="brand", method="brand_filter")
    category = filters.CharFilter(label="category", method="category_filter")

    def brand_filter(self, queryset, name, value, *args, **kwargs):
        print("brand_filter called", value)
        if "," in value:
            value = value.split(",")
            return queryset.filter(brand_id__in=value)
        return queryset.filter(brand_id=value)

    def category_filter(self, queryset, name, value: str, *args, **kwargs):
        print("category_filter called", value)
        if "," in value:
            value = value.split(",")
            return queryset.filter(_category__in=value)
        return queryset.filter(_category=value)

(brand is foreignkey, _category is integerfield)
Also it calls brand_filter method but I don't understand why it prints not found. Do I need to configure something for relation filters?

Hi @bertek41 — you need to reduce this somewhat for me to be able to see what's going on here. I've read it a few times but I'm very much 🤷

Hi, so I went from 22.1 to 24.2 and in my tests when using relation filter it returns 404.

Yes, I get that. But why is the filter failing? You need to dig in to see what's happening there.

Do you have any suggestions where I should start digging?

So... you can look at the filterset's form (for instance) Is it giving you the expected value out of cleaned data? You can put a breakpoint in your filtering methods and see what's actually happening there. And so on. How do you normally debug an issue? Like that.