G
G
Grigory Dikiy2017-06-14 13:33:39
Django
Grigory Dikiy, 2017-06-14 13:33:39

Filter problems?

Good afternoon! I'm trying to implement filters, but for some reason it doesn't work. In the view file, I have a class for displaying products:

class CategoryView(ListView):
    template_name = '__index.html'
    context_object_name = 'products'
    paginate_by = 20

    def get_queryset(self):
        queryset = Product.objects.filter(category__slug=self.kwargs.get('slug')).order_by('-created')

        request = self.request

        # Filter By Brand and Country
        if request.GET.get('country'):
            print(request.GET.get('country'))
            queryset.filter(brand__country__slug=request.GET.get('country'))

        if request.GET.get('brand'):
            print(request.GET.get('brand'))
            queryset.filter(brand__slug=request.GET.get('brand'))

        print(queryset[0].brand.slug)
        print(queryset[0].brand.country.slug)

        return queryset

In the get_queryset function, I try to filter by brand and country, but the result is not what you expect:
That is, where there is print, the following is displayed when querying: /?brand=astra-gold&country=chehiya
chehiya
astra-gold
veneto
italiya

Obviously, chehiya is not equal to italiya neither is veneto astra-gold, but why filters is a mystery to me.
I would appreciate your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-06-14
@frilix

Instead of
It is necessary to write:

queryset = queryset.filter(brand__country__slug=request.GET.get('country'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question