0

I tried to create a question filtering function dependent on values from request: question_type and situation. If both predicates provided, I want Questions filterd by both values. If only one of them - I want Questions filtered by provided filter only.

However, no matter what filter values I send in question_type or situation arguments, all questions are returned, no filtering.

models.py

class Question(models.Model):
    TYPES_CHOICES = [
        ("LAWYER","LAWYER"),
        ("REPAIRMAN","REPAIRMAN"),
        
    ]
    SITUATION_CHOICES = [
        ("in","IN"),
        ("wait","WAIT"),
        ("closed","CLOSED")
    ]
    question_type = models.CharField(max_length=255,choices=TYPES_CHOICES,default="LAWYER")
    title = models.CharField(max_length=255)
    description = models.TextField()
    image = models.ImageField(upload_to="question_images/",blank=True,null=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    situation = models.CharField(max_length=20,choices=SITUATION_CHOICES,default="in")

views.py

class TypeView(ListAPIView):
    serializer_class = QuestionSerializer

    def get_queryset(self):
        qs = Question.objects.all()
        question_type = self.request.query_params.get('question_type')
        situation = self.request.query_params.get('situation')
        
        if question_type and situation:
            qs = qs.filter(question_type=question_type, situation=situation)
        elif question_type:
            qs = qs.filter(question_type=question_type)
        elif situation:
            qs = qs.filter(situation=situation)
            
        return qs

urls.py

    path('search/<str:question_type>/<str:situation>/', views.TypeView.as_view(), name='filtered-question'),
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
  • *What* didn't work? – Willem Van Onsem Apr 22 '23 at 11:03
  • No matter what value I sent as question type or situation, all questions are sent, there is no filtering. – Berk Akalın Apr 22 '23 at 12:13
  • 1
    `query_params` are these things: `search?question_type=a&situation=b%20c` - arguments after `?`. You're not using _query params_ thus `.query_params` is always empty. Params from url_pattern are stored in `kwargs` [see the docs](https://www.django-rest-framework.org/api-guide/filtering/#filtering-against-the-url) – Ivan Starostin Apr 22 '23 at 13:29
  • Does this answer your question? [Capture parameters in django-rest-framework](https://stackoverflow.com/questions/21292646/capture-parameters-in-django-rest-framework) – Ivan Starostin Apr 22 '23 at 13:33

1 Answers1

0

The issue here is that you are using URL path parameters instead of query parameters in your URL configuration.

Update your urls.py as follows.

path('search/', views.TypeView.as_view(), name='filtered-question'),

In your TypeView class in views.py, you are already reading the query parameters correctly using self.request.query_params.get(). So, you don't need to change anything in the get_queryset() method.

You should now be able to make requests such as these:

/search/?question_type=LAWYER&situation=in
/search/?question_type=LAWYER
/search/?situation=in
ismaael17
  • 136
  • 5