carltongibson / django-filter

A generic system for filtering Django QuerySets based on user selections
https://django-filter.readthedocs.io/en/main/
Other
4.45k stars 766 forks source link

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

Closed bertek41 closed 1 month ago

bertek41 commented 3 months ago

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?

carltongibson commented 3 months ago

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 🤷

bertek41 commented 3 months ago

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

carltongibson commented 3 months ago

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

bertek41 commented 3 months ago

Do you have any suggestions where I should start digging?

carltongibson commented 3 months ago

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.

bertek41 commented 1 month ago

Looks like django needs name assignment in urls.py